How to close popup window by button in javafx - java

I'm trying to close popup window by button, but I don't have any idea.
When I used Java Swing, I remember the function was automatic...
So.. What should I do?
And I also want to make more space between button and text. If you have any idea, please help me.
Stage dialogStage = new Stage();
dialogStage.initModality(Modality.WINDOW_MODAL);
Button button = new Button();
button.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
if (event.getCode() == KeyCode.ENTER) {
}
}
});
VBox vbox = new VBox(new Text("Wrong ID & PW!!"), new Button("Ok."));
vbox.setAlignment(Pos.CENTER);
vbox.setPadding(new Insets(15));
dialogStage.setScene(new Scene(vbox));
dialogStage.show();

Call dialogStage.close()
button.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
if (event.getCode() == KeyCode.ENTER) {
dialogStage.close();
}
}
});
Also you are adding a new Button("Ok.") to the VBox which is wrong, add the button which you created before
As for the space between the button and text, this should work
VBox.setMargin(text, new Insets(20));
VBox.setMargin(button, new Insets(20));

Related

How to change position of button JavaFX

As you can see I have two buttons right now (button and button2) The problem is that they are in the center of JavaFX window (in the same place). So I don't see one of them because 2nd is covering 1st. What is wrong? The main objective of this simple app is to have two buttons on main screen. And after clicking into one of buttons, I expect to open new window with some other content.
public class Run extends Application {
#Override
public void start(final Stage primaryStage) {
Button button = new Button();
button.setText("Navigation");
button.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Label secondLabel = new Label("Select from list");
StackPane secondaryLayout = new StackPane();
secondaryLayout.getChildren().add(secondLabel);
Scene secondScene = new Scene(secondaryLayout, 500, 400);
// New window (Stage)
Stage newWindow = new Stage();
newWindow.setTitle("Adding products");
newWindow.setScene(secondScene);
// Specifies the modality for new window.
newWindow.initModality(Modality.WINDOW_MODAL);
// Specifies the owner Window (parent) for new window
newWindow.initOwner(primaryStage);
// Set position of second window, related to primary window.
newWindow.setX(primaryStage.getX() + 200);
newWindow.setY(primaryStage.getY() + 100);
newWindow.show();
}
});
Button button2 = new Button();
button2.setText("X");
button2.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Label secondLabel = new Label("Select from list");
StackPane secondaryLayout = new StackPane();
secondaryLayout.getChildren().add(secondLabel);
Scene secondScene = new Scene(secondaryLayout, 500, 400);
// New window (Stage)
Stage newWindow = new Stage();
newWindow.setTitle("Adding products");
newWindow.setScene(secondScene);
// Specifies the modality for new window.
newWindow.initModality(Modality.WINDOW_MODAL);
// Specifies the owner Window (parent) for new window
newWindow.initOwner(primaryStage);
// Set position of second window, related to primary window.
newWindow.setX(primaryStage.getX() + 200);
newWindow.setY(primaryStage.getY() + 100);
newWindow.show();
}
});
StackPane root = new StackPane();
root.getChildren().add(button);
root.getChildren().add(button2);
Scene scene = new Scene(root, 650, 450);
primaryStage.setTitle("Shopping Buddy");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Cannot Maximize Window with JavaFX

I made a custom Minimize button this way:
public MinimizeButton() {
Button button = new Button("-");
button.getStyleClass().clear();
button.getStyleClass().add("actionbutton");
button.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Stage stage = (Stage) ((Button) event.getSource()).getScene().getWindow();
stage.setIconified(true);
}
});
this.getChildren().add(button);
}
And I obviously called
primaryStage.initStyle(StageStyle.UNDECORATED);
The button is working well.
The issue is that when I try to maximize the Window once the Stage is iconified, it takes a couple of seconds for the Window to redraw the Stage.
Any ideas on how to make the "Maximizing process" of the Window faster?
Fixed it by using
primaryStage.initStyle(StageStyle.TRANSPARENT);
instead of
primaryStage.initStyle(StageStyle.UNDECORATED);

Java FX - Removing a node if the user unchecks a checkbox

I am making a GUI using JavaFx, I want a name field to appear if the check box is checked and disappear if it is not checked.
Here is the code I wrote
test1.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Boolean b = test1.isSelected();
log(b);
Label label1 = new Label("Name:");
TextField textField = new TextField();
HBox hb = new HBox();
if (test1.isSelected()) {
hb.getChildren().addAll(label1, textField);
hb.setSpacing(10);
grid.add(hb, 3, 3);
} else {
**hb.getChildren().removeAll(label1 , textField);** <---look at this!
}
}
});
I am removing the node if it is unchecked but it doesn't execute it accordingly. What am I doing wrong?
Incase it is required, here is how I execute my scene.
Scene scene = new Scene(grid, 500, 500);
primaryStage.setScene(scene);
primaryStage.show();

Closing a modal window by clicking button

I have a window with a button. Clicking this button opens a modal window.
Now, I want to close this second window by clicking a button, but I can't figure out how.
public class StartMenu extends Application {
#Override
public void start(Stage primaryStage) {
final Button b = new Button("Go");
b.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
AnotherWindow aw = new AnotherWindow ();
aw.start(stage);
}
});
((Group) scene.getRoot()).getChildren().add(b);
primaryStage.setScene(scene);
primaryStage.show();
}}
 
 
public class AnotherWindow extends Application {
#Override
public void start(Stage primaryStage) {
final Button b = new Button("Back");
b.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
//Code to close window
}
});
((Group) scene.getRoot()).getChildren().add(b);
primaryStage.setScene(scene);
primaryStage.show();
}}
I found the following post by Krzysztof Sz. that helped me find the solution.
public class AnotherWindow extends Application {
#Override
public void start(Stage primaryStage) {
final Button b = new Button("Back");
b.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
((Button)t.getTarget()).getScene().getWindow().hide();
}
});
((Group) scene.getRoot()).getChildren().add(b);
primaryStage.setScene(scene);
primaryStage.show();
}}
 
It is the following piece of code that let's me close the current (modal) window when the button is clicked:
((Button)t.getTarget()).getScene().getWindow().hide();
You want to close a modal window from a click on a different window? If a modal window is visible, how will you get back to the other window?
You might want to use one window: when a button is clicked, hide all the controls in that window and make visible the information you wanted to have in your modal window, along with a button to click. When that button is clicked, reset the window to its' original state.
This just becomes an exercise in showing/hiding controls in a container.

Creating multiple buttons in java scene

I am attempting to create a scene with multiple buttons and I am having some issues.
What I have now is this:
public class Tester extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
Button btn1 = new Button();
btn1.setText("Start Game");
Button btn2 = new Button();
btn2.setText("Exit");
btn2.setOnAction(new EventHandler<ActionEvent>());
btn1.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Game Start");
}
});
Pane root = new Pane();
btn1.setLayoutX(500);
btn1.setLayoutY(530);
root.getChildren().add(btn1);
primaryStage.setScene(new Scene(root, 1024, 768));
primaryStage.show();
I am trying to figure out what I need to do to have a second button. At the moment, I cant seem to have a second event handler.
Any help will be greatly appreciated.
Here is an update to your program to:
Define an action (display "Wumpus Hunt Complete!") for the second button.
Add the second button to the scene so that it can be seen.
The event handler for taking action for a button is an example of an anonymous inner class.
Sample Code:
Button btn1 = new Button();
btn1.setText("Start Game");
Button btn2 = new Button();
btn2.setText("Exit");
btn2.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Wumpus Hunt Complete!");
}
});
btn1.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Game Start");
}
});
Pane root = new Pane();
btn1.setLayoutX(500);
btn1.setLayoutY(530);
root.getChildren().add(btn1);
btn2.setLayoutX(500);
btn2.setLayoutY(630);
root.getChildren().add(btn2);
primaryStage.setScene(new Scene(root, 1024, 768));
primaryStage.show();

Categories