1

I don't know how I can switch scenes after pressing testButton. Start method doesn't allow me to do anything with testStage. I mean I can't make this my new Main Stage or anything.

public class GUI extends Application
{

    public static void initialize(String[] args)
    {
        launch(args);
    }
    public static Stage testStage;

    @Override
    public void start(Stage mainStage) throws Exception
    {
        Scene scene = null;
        scene = GUIMainScene.setScene();

        mainStage.setScene(scene);
        mainStage.show();
    }

}

This is my second class or rather some lines from this class:

public class GUIMainScene
{

    public static Scene setScene()
    {
        setLabel();
        setButtons();
        setLayout();
        return new Scene(layout);
    }

    private static void setAddingButton()
{
    testButton.setText("Give us a dog");
    testButton = setButtonSize(testButton);
    testButton.setOnAction(e -> GUI.testStage.setScene(GUITestScene.setScene()));

}
}
konrad2645
  • 25
  • 7
  • 2
    Possible duplicate of [Passing Parameters JavaFX FXML](http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml) – purring pigeon May 15 '17 at 17:13
  • Hi! Don't forget to specify error message you get. I suspect it will be about lambda not being supported with your language level. – Hugues M. May 15 '17 at 17:19
  • Lambda is being supported with my language level. I made tests with just one class. Everything worked fine. – konrad2645 May 15 '17 at 17:22

1 Answers1

0

There are few simple solutions, you could pass primaryStage and change scene inside other class, or pass scene and change root node, still I think that the best way would be to use SimpleBooleanProperty/SimpleIntegerProperty. This allows you to set change listener and based on that you can control which gui is shown from main class without passing anything from it.

Example:

public class GuiClass {

private SimpleBooleanProperty guiSwitch;

public GuiClass() {
    guiSwitch = new SimpleBooleanProperty(false);
}
public SimpleBooleanProperty getGuiSwitchProp() {
    return guiSwitch;
}
public StackPane getGui() {
    StackPane root = new StackPane();
    Button btn = new Button("Click me");     
    root.getChildren().add(btn);
    btn.setOnMouseClicked(ev -> setGuiSwitch(true));
    return root;
}
public void setGuiSwitch(Boolean value) {
    this.guiSwitch.set(value);
}
public boolean getGuiSwitch() {
    return this.guiSwitch.get();
}
}

Then in your main:

public class GUI extends Application
{

public static void initialize(String[] args)
{
    launch(args);
}

@Override
public void start(Stage mainStage) throws Exception
{
    GuiClass gui1 = new GuiClass();
    GuiClass gui2 = new GuiClass();

    gui1.getGuiSwitchProp().addListener(listener -> {
        if (gui1.getGuiSwitch()) {
            scene.setRoot(gui2.getGui());
            gui1.setGuiSwitch(false);
        }
    });
    gui2.getGuiSwitchProp().addListener(listener -> {
        if (gui2.getGuiSwitch()) {
            scene.setRoot(gui1.getGui());
            gui2.setGuiSwitch(false);
        }
    });

    Scene scene = new Scene(gui.getGui(), 300, 300);
    mainStage.setScene(scene);
    mainStage.show();
}

}

You can maybe pass text for button in GuiClass to see that it is actually changing, if you would like to have more options for switching scenes you can use SimpleIntegerProperty and then have 0 default, -1 to go back, 1 to go on next one etc.

FilipRistic
  • 2,661
  • 4
  • 22
  • 31
  • Thanks, I haven't heard about SimpleBooleanProperty and SimpleIntegerProperty, but I understand your code and I am going to try this solution. – konrad2645 May 15 '17 at 17:51
  • Make sure to read documentation about them, it is quite useful thing to know when working with javafx and can save you a lot of trouble. – FilipRistic May 15 '17 at 18:31