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();
Related
public class Testing extends Application {
#Override
public void start(Stage stage)
{
Button button1 = new Button("First button");
Button button2 = new Button("Second button");
EventHandler<ActionEvent> aHandler = new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent event)
{
button2.setText("Working");
}
};
button1.addEventHandler(ActionEvent.ACTION, aHandler);
HBox hbox = new HBox(40,button1, button2);
Scene scene = new Scene(hbox, 840, 400);
stage.setScene(scene);
stage.setTitle("Testing");
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
You can see that this is a javafx Testing class where I am testing eventHandlers and it works fine but when I split the code and add it into on its own methods then the eventHandlers does not work like in the code below
public class Testing extends Application {
#Override
public void start(Stage stage)
{
EventHandler<ActionEvent> aHandler = new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent event)
{
button2().setText("Working");
}
};
button1().addEventHandler(ActionEvent.ACTION, aHandler);
stage.setScene(scene());
stage.setTitle("Testing");
stage.show();
}
public Button button1()
{
Button btn = new Button("First button");
return btn;
}
public Button button2()
{
Button btn = new Button("Second button");
return btn;
}
public HBox hbox()
{
HBox hbox = new HBox(40,button1(), button2());
return hbox;
}
public Scene scene()
{
Scene scene = new Scene(hbox(), 840, 400);
return scene;
}
public static void main(String[] args)
{
launch(args);
}
}
Now this code does not work. Please help.
Please note: If any one have another idea to encapsulate eventHandlers then please mention it if you can because my goal is to define eventHandlers in one class and registering it in another class.
Thank you.
Of course it's not working, you are creating an instance of Button every call to button1() and button2(). The instance of button1 and button2 in the HBox are different instances from the one you added the event handler.
I definitely recommend not splitting like what you are doing. This kind of splitting makes it hard to troubleshoot problems, and you are creating new instances whenever you call any of those methods. Stick to what you are doing originally.
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);
}
}
I have managed to place a Button inside a TitledPane using setGraphic but I want it to be located on the right side; here is the code:
#Override
public void start(Stage primaryStage) {
Accordion acordion = new Accordion();
TitledPane tp1 = new TitledPane();
Button b1 = new Button();
b1.setText("X");
tp1.setGraphic(b1);
acordion.getPanes().addAll(tp1);
Scene scene = new Scene(acordion, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
The Button should be located on the right but I do not know how to do it
Set the alignment property of TitledPane to CENTER_RIGHT:
tp1.setAlignment(Pos.CENTER_RIGHT);
I'm having a problem with creating a method and calling it when a button is clicked.
What I want to do is when a button is clicked the scene changes to a new one. I have already achieved this but I came across a problem when I need to create more of such actions.
Here is my code:
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
BorderPane borderPane = new BorderPane();
Button dugmeStart = new Button("Start");
dugmeStart.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
BorderPane borderPane2 = new BorderPane();
Label prvo_pritanje = new Label("Prvo pitanje: Kada je poceo Prvi svetski rat?");
prvo_pritanje.setStyle("-fx-font-weight: bold;");
Button dalje1 = new Button("Dalje");
//Navigacija
HBox navigacija_goranj = new HBox();
navigacija_goranj.getChildren().add(prvo_pritanje);
navigacija_goranj.setAlignment(Pos.CENTER);
navigacija_goranj.setPadding(new Insets(15,12, 15, 12));
HBox navigacija_donja = new HBox();
navigacija_donja.getChildren().add(dalje1);
navigacija_donja.setPadding(new Insets(15,12, 15, 12));
navigacija_donja.setAlignment(Pos.CENTER);
//Odgovori
HBox odgovori = new HBox();
ToggleGroup pitanja1 = new ToggleGroup();
RadioButton prvo_pitanje = new RadioButton("1914");
prvo_pitanje.setToggleGroup(pitanja1);
RadioButton drugo_pitanje = new RadioButton("1918");
drugo_pitanje.setToggleGroup(pitanja1);
RadioButton trece_pitanje = new RadioButton("1910");
trece_pitanje.setToggleGroup(pitanja1);
odgovori.getChildren().addAll(prvo_pitanje, drugo_pitanje, trece_pitanje);
odgovori.setSpacing(15);
odgovori.setAlignment(Pos.CENTER);
//Dodavanje u BorderPane
borderPane2.setBottom(navigacija_donja);
borderPane2.setTop(navigacija_goranj);
borderPane2.setCenter(odgovori);
Scene scena2 = new Scene(borderPane2, 300, 300);
primaryStage.setScene(scena2);
}
});
Label dobrodoslica = new Label("Pocetak kviza");
dobrodoslica.setStyle("-fx-font-weight: bold;");
//Definisanje Hbox-a sa dugmetom "Start"
HBox navigacija1 = new HBox();
navigacija1.setPadding(new Insets(15, 12, 15, 12));
navigacija1.getChildren().add(dugmeStart);
navigacija1.setAlignment(Pos.CENTER);
borderPane.setBottom(navigacija1);
borderPane.setCenter(dobrodoslica);
Scene scene = new Scene(borderPane,300,300);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("Domaci: Kviz");
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
You can see that when a button is clicked it creates a whole new scene and BorderPane, and populates it with content. I figured that it would be best to create a method with each scene and then call each method when a button is clicked. The problem is I cant figure out how to do this.
Any help is appreciated. Thanks in advance.
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.