I'm trying to learn javafx and I'm having some trouble with alert box interactions. My code is pretty straightforward:
public static void display(String title, String message) {
Stage window = new Stage();
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle(title);
window.setMinWidth(250);
Label label = new Label();
label.setText(message);
Button closeButton = new Button("Close the window");
closeButton.setOnAction(e -> window.close());
VBox layout = new VBox(10);
layout.getChildren().addAll(label, closeButton);
layout.setAlignment(Pos.CENTER);
Scene scene = new Scene(layout);
window.setScene(scene);
window.showAndWait();
}
The problem is I can still somewhat interact with my initial window that I used to open the alert box, mainly clicking on the initial window brings it in front of the alert box. To my understanding, this shouldn't happen. Here is a gif demonstrating the issue: https://gyazo.com/0c2b69ec39f849227560fbdf2099c07c
Here is my code that calls the alert box
public void start(Stage primaryStage) {
window = primaryStage;
window.setTitle("Alert Box test");
button = new Button("Click me");
button.setOnAction(e -> AlertBox.display("New Alert", "Don't forget to close this window!"));
StackPane layout = new StackPane();
layout.getChildren().add(button);
Scene scene = new Scene(layout, 300, 250);
window.setScene(scene);
window.show();
}
What am I doing wrong here or is that just the intended behavior?
Related
I have two scenes A and B and a stage.
If I'm in scene A, and change the stage width / height, it will update the UI to scale it to the new stage dimensions.
However if from there I switch to scene B, the UI in scene B will not be updated till I force a update by doing something like resizing the screen or opening and closing the stage.
Is there a way to update all scenes when the stage width / height is changed? Since only one scene can be set to the stage at one time the current changes are not applying automatically to all scenes.
Here's a minimal reproduced example.
#Override
public void start(Stage stage) throws Exception {
stage.setWidth(400);
stage.setHeight(400);
Scene scene1, scene2;
BorderPane layout1 = new BorderPane();
Button buttontosecond = new Button("go to second page");
layout1.setCenter(buttontosecond);
scene1 = new Scene(layout1, stage.getWidth(), stage.getHeight());
BorderPane layout2 = new BorderPane();
Button buttonsize = new Button("change size");
Button buttontomain = new Button("back to main");
VBox vbox = new VBox(buttonsize, buttontomain);
vbox.setAlignment(Pos.CENTER);
layout2.setCenter(vbox);
scene2 = new Scene(layout2, stage.getWidth(), stage.getHeight());
buttontomain.setOnAction(actionEvent -> {stage.setScene(scene1);});
buttontosecond.setOnAction(actionEvent -> {stage.setScene(scene2);});
buttonsize.setOnAction(actionEvent -> {stage.setWidth(200);stage.setHeight(200);});
stage.setScene(scene1);
stage.show();
}
DO not do scene switching in this way. Instead implement a single scene root-switching system as shown in the answer to this post. I did not manage to get the code above to work, but I found that the reason for this was my dual monitor setup. When dragging the game window into the other monitor it worked fine.
I have two buttons: buttonPoint1 and buttonPoint2. When I click one of the buttons it calls action method for this button. In this function I want program to wait for me to click wherever on a screen. Then I'll call MouseInfo.getPointerInfo().getLocation(). When I click outside my app window (e.g on other window), I lose focus on my app. How to make program to wait for click, don't hide, lose focus and give me response that I clicked?
One idea is using a transparent window like so
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
VBox vBox = new VBox(new Label("IMAGINE A BEAUTIFUL APPLICATION HERE"));
vBox.setAlignment(Pos.CENTER);
Button button = new Button("Im gonna steal your next click");
button.setOnAction(event -> {
System.out.println("Firing Button Action");
openTransparentWindow();
System.out.println("Button Action Finished Firing");
});
vBox.getChildren().add(button);
Scene scene = new Scene(vBox, 300, 150);
primaryStage.setScene(scene);
primaryStage.show();
}
private void openTransparentWindow(){
VBox vBox = new VBox();
Stage stage = new Stage();
stage.setScene(new Scene(vBox));
//stage.setOpacity(0.5f); //Uncomment if you want to test and see the stage
stage.setOpacity(0f);
stage.setMaximized(true);
vBox.setOnMouseClicked(event ->{
System.out.println("event.getX() = " + event.getX());
System.out.println("event.getY() = " + event.getY());
System.out.println("event.getScreenX() = " + event.getScreenX());
System.out.println("event.getScreenY() = " + event.getScreenY());
stage.close();
});
stage.showAndWait();
}
}
I'm trying to create a new Stage when a button is pressed.
It works but the problem is that I'd like this Stage to be fully transparent and lets us see what's behind the screen.
Code
Dimension Sizescreen = Toolkit.getDefaultToolkit().getScreenSize();
//Main stage with option menu
Pane window = new Pane();
Scene scene = new Scene(window);
stage.setTitle("Notification Extender");
//Create the button SetLooker
Button SetLooker = new Button("Set Looker");
//Add a Event when pressed
SetLooker.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
//Create a sub-Stage
Pane subwindow = new Pane();
Scene subscene = new Scene(subwindow);
Stage substage = new Stage();
substage.setTitle("Notification Extender");
//Set this subStage Transparent
substage.initStyle(StageStyle.TRANSPARENT);
subscene.setFill(Color.TRANSPARENT);
substage.setWidth(Sizescreen.getWidth());
substage.setHeight(Sizescreen.getHeight());
substage.setX(0);
substage.setY(0);
//Create a a graphique element
Rectangle redrec = new Rectangle(120,40,50,50);
redrec.setStroke(Color.RED);
redrec.setStrokeWidth(2);
redrec.setFill(Color.TRANSPARENT);
//Add the graphique element to the sub-stage
subwindow.getChildren().add(redrec);
//Show the sub-stage
substage.setScene(subscene);
substage.show();
}
});
//Add the button to the main stage
window.getChildren().add(SetLooker);
//Show the main stage
stage.setScene(scene);
stage.show();
The problem is that when I press the button it shows the stage but it's not transparent at all it's completely white.
I've also tried to change the main Stage, but I cannot change it once it has been shown.
You also need to remove the background from the root of your new scene:
subwindow.setBackground(null);
I made a very simple java application with 3 scenes and 3 buttons.
button3 (b3) opens a popup but I set the image to lock size but the window can still be changed by using the mouse on one of the corners.
is there a way to lock the size so the curson can't edit this?
Here's the simplified code (just to get and idea)
public class Handler extends Application
{
#Override
public void start(Stage primaryStage) throws Exception {
Button button1 = new Button("AD");
VBox layout1 = new VBox(button1);
layout2.setAlignment(Pos.CENTER);
Pane layout2 = new Pane();
layout3.setStyle("-fx-background-image: url(ad.jpg);" +
"-fx-background-repeat: no-repeat;" +
"-fx-background-size: initial;");
Scene scene1 = new Scene(layout1);
Scene scene2 = new Scene(layout2);
//Stage (window)
primaryStage.setTitle("Basic Handler");
primaryStage.setScene(scene1);
primaryStage.setHeight(400);
primaryStage.setWidth(400);
primaryStage.show();
button1.setOnAction(event -> {
Stage PopUp = new Stage();
PopUp.setHeight(266);
PopUp.setWidth(474);
PopUp.setTitle("Sponsored AD");
PopUp.setScene(scene2);
PopUp.showAndWait();
});
}
}
Just add this in your stage :
PopUp.setResizable(false);
I am basically new to Java FX 2.
Scenario:
I have 3 Scenes and I want a way to add menu-bar such that I don't i don't want to explicitly remove the menu bar from previous scene and add it to new one. Like Some thing a Parent Scene or some way menu-bar is attached to Stage. I mean menu-bar is added just one time and always be present whatever scene is in front or not.
If This is Possible How Can I do this.
Here is the Default Example Provided by Oracle Docs of JavaFX http://docs.oracle.com/javafx/2/ui_controls/MenuSample.java.html
public class Main extends Application {
final ImageView pic = new ImageView();
final Label name = new Label();
final Label binName = new Label();
final Label description = new Label();
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
stage.setTitle("Menu Sample");
Scene scene = new Scene(new VBox(), 400, 350);
scene.setFill(Color.OLDLACE);
MenuBar menuBar = new MenuBar();
// --- Graphical elements
final VBox vbox = new VBox();
vbox.setAlignment(Pos.CENTER);
vbox.setSpacing(10);
vbox.setPadding(new Insets(0, 10, 0, 10));
makeContentsForVBox();// in this vBox area will be fill with name pic desrciption
vbox.getChildren().addAll(name, binName, pic, description); // name is lable
// --- Menu File
Menu menuFile = new Menu("File");
MenuItem add = new MenuItem("Shuffle",
new ImageView(new Image(getClass().getResourceAsStream("new.png"))));
add.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
shuffle();
vbox.setVisible(true);
}
});
MenuItem clear = new MenuItem("Clear");
clear.setAccelerator(KeyCombination.keyCombination("Ctrl+X"));
clear.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
vbox.setVisible(false);
}
});
MenuItem exit = new MenuItem("Exit");
exit.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
System.exit(0);
}
});
menuFile.getItems().addAll(add, clear, new SeparatorMenuItem(), exit);
((VBox) scene.getRoot()).getChildren().addAll(menuBar, vbox);
stage.setScene(scene);
stage.show();
}
}
So Here menuBar is added to a scene. if i swap the scene and bring an other scene in front ... What will i do. i think I remove menuBar from this scene and add to other or simply add to new one. so every time i have to do this when i change. Is there any way to avoid this??
The approach I would prefer is to use a Scene with BorderPane as its root
scene.setRoot(borderPane);
You can add the MenuBar to the top of the BorderPane and at its Center you can place SplitPane
BorderPane borderPane = new BorderPane();
borderPane.setTop(menuBar);
borderPane.setCenter(splitPane);
Whenever you need to switch to WebView just replace it with SplitPane :
borderPane.setCenter(webView);
Following this approach, your MenuBar will always remain on TOP and you can switch between SplitPane and WebView