0

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.

Gilbert
  • 93
  • 15
  • possible duplicate of http://stackoverflow.com/questions/10800678/can-i-access-new-methods-in-anonymous-inner-class-with-some-syntax - as long you make an anonymous class that extends EventHandler you won't be able to add new methods - but you can just make a fully fledged(or an inner) class which extends EventHandler and create an object of that class and pass it to `dugmeStart.setOnAction` – Japu_D_Cret Mar 29 '17 at 14:26
  • 1
    Just write a method and call it from the `handle()` method.... it's not really clear what the question is here. – James_D Mar 29 '17 at 14:40
  • Hi. The problem is that I am quite new to Java and don't understand what you are trying to tell me. How do I create fully fledged class which extends EventHandler? Sorry if I'm to vague. – Gilbert Mar 29 '17 at 14:45
  • You don't need to change the structure of how you define the event handler. Just create a method in `Main` (or wherever you want it), and then call it from the `handle()` method). Again, not sure what you are asking. You should try that and post your attempt to do it in the question if you can't get it to work. – James_D Mar 29 '17 at 21:27

0 Answers0