Hey I searched the net for quite a while but I couldn't find a solution to the following problem:
In javafx you got 3 basic files; the controller-class, the fxml file and the application class. Now I want to react in the controller to a button-click (which works perfectly fine) and change the screen on that click (which you normally do with stage.setScreen() ), but I have no reference to the stage (which you can find in the application class).
Application-Sample:
public class JavaFXApplication4 extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
FXML-Sample:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200.0" prefWidth="320.0" xmlns:fx="http://javafx.com/fxml" fx:controller="javafxapplication4.SampleController">
<children>
<Button id="button" fx:id="nextScreen" layoutX="126.0" layoutY="90.0" onAction="#handleButtonAction" text="Next Screen" />
<Label fx:id="label" layoutX="126.0" layoutY="120.0" minHeight="16.0" minWidth="69.0" />
</children>
</AnchorPane>
Controller-Sample:
public class SampleController implements Initializable {
@FXML
private Label label;
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
//Here I want to swap the screen!
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
I would be thankful for any kind of help.
I found this old question while getting into Java and trying to solve the same thing. Since I wanted the scenes to remember the content between the switches, I couldn't use the accepted answer, because when switching between the scenes it instantiates them again (loosing their previous state).
Anyways the accepted answer and the answer to the similar question gave me a hints on how to switch the scenes without loosing their states. The main idea is to inject instance of the scene into another controller, so that the controller doesn't need to instantiate a fresh scene over and over again but can use already existing instance (with its state).
So here is the main class that instantiates the scenes:
And here are the both controllers:
yep, second one looks the same (some logic could probably be shared, but the current state is enough as a proof of concept)
You can try like this too.