JavaFX FXML
- Open and close Stage (and scene) from within different controllers. Scenes are stored in a Hashmap
- Stage venster openen (met scene opgeslagen in Hashmap) en sluiten vanuit Controllers
1: Start_scenes.java
package yourpackage;
import java.net.URL;
import java.util.HashMap;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Start_scenes extends Application {
@FXML
public static HashMap<String, Scene> scenes = new HashMap<String, Scene>();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
URL location01 = getClass().getResource("Scene01.fxml");
URL location02 = getClass().getResource("Scene02.fxml");
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
fxmlLoader.setRoot(fxmlLoader.load(location01.openStream()));
Scene scene = new Scene((Parent) fxmlLoader.getRoot());
scenes.put("scene01", scene);
fxmlLoader.setRoot(null);
fxmlLoader.setController(null);
//fxmlLoader.setLocation(null);
fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
fxmlLoader.setRoot(fxmlLoader.load(location02.openStream()));
Scene scene01 = new Scene((Parent) fxmlLoader.getRoot());
scenes.put("scene02", scene01);
stage.setScene(scenes.get("scene02"));
stage.show();
}
}
2: Scene01Controller.java
package yourpackage;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
* @auteur pw : Belgium (02-2013)
*/
public class Scene01Controller extends Stage implements Initializable {
@FXML
private void handleButtonAction(ActionEvent event) {
// English : Get the scene to be displayed
// Belgium : Haal de gewenste scene op
Scene scene = this.getScene();
scene = Start_scenes.scenes.get("scene02");
Node source = (Node) event.getSource();
Stage stage = (Stage) source.getScene().getWindow();
stage.close();
this.setScene(scene);
this.show();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
3: Scene02Controller.java
package yourpackage;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
* @auteur pw Belgium
*/
public class Scene02Controller extends Stage implements Initializable {
@FXML
private void handleButtonAction(ActionEvent event) {
// English : Get the scene to be displayed
// Belgium : Haal de gewenste scene op
Scene scene = this.getScene();
scene = Start_scenes.scenes.get("scene01");
Node source = (Node) event.getSource();
Stage stage = (Stage) source.getScene().getWindow();
stage.close();
this.setScene(scene);
this.show();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
4: Scene01.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane id="AnchorPane" prefHeight="200.0" prefWidth="320.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="meerderescenes.Scene01Controller">
<children>
<Button fx:id="button" layoutX="22.0" layoutY="104.0" onAction="#handleButtonAction" text="Close this window and open window scene 02" />
<Label layoutX="102.0" layoutY="33.0" text="SCENE 01">
<font>
<Font size="22.0" />
</font>
</Label>
<Label layoutX="91.0" layoutY="155.0" text="Opgehaald uit Hashmap" />
</children>
</AnchorPane>
5: Scene02.java
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane id="AnchorPane" prefHeight="200.0" prefWidth="320.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="meerderescenes.Scene02Controller">
<children>
<Button fx:id="button" layoutX="21.0" layoutY="90.0" onAction="#handleButtonAction" prefWidth="285.0" text="Sluit dit venster en open venster scene 01" />
<Label layoutX="90.0" layoutY="37.0" text="SCENE 02">
<font>
<Font size="30.0" />
</font>
</Label>
<Label layoutX="76.0" layoutY="145.0" text="Haal scene 01 op uit Hashmap" />
</children>
</AnchorPane>
|