After much searching I found this question How to create a javafx 2.0 application MDI. What I really wanted to know is if I can create a pop-up window or child window to the main window using JavaFX components and Scene Builder to create the new window.
I ended up with this for a modal pop-up window:
In the Main class I wanted to save the primary stage to a field I can access from my primary controller class. So, I added a static variable Stage to it and this in the Main.Start() method:
primaryController.primaryStage = primaryStage;
This the method that a button in the primaryController uses:
public void OnBtnShowChild(ActionEvent event) {
MessageBoxController msgBox = new MessageBoxController();
try {
msgBox.showMessageBox(primaryStage);
} catch (Exception e) {
e.printStackTrace();
}
}
This is the MessageBoxController class that I created with help from Scene Builder. It has the basic layout of a standard pop-up box that can be used to display an Icon (ImageView), TextBox (for your message text), and two buttons (for YES/NO functionality). I am not sure yet how to have it communicate the results of what button was pressed back to the primaryController.
public class MessageBoxController implements Initializable {
#FXML
// fx:id="btnNo"
private Button btnNo; // Value injected by FXMLLoader
#FXML
// fx:id="btnYes"
private Button btnYes; // Value injected by FXMLLoader
#FXML
// fx:id="imgMessage"
private ImageView imgMessage; // Value injected by FXMLLoader
#FXML
// fx:id="txtMessage"
private TextField txtMessage; // Value injected by FXMLLoader
private Stage myParent;
private Stage messageBoxStage;
public void showMessageBox(Stage parentStage) {
this.myParent = parentStage;
try {
messageBoxStage = new Stage();
AnchorPane page = (AnchorPane) FXMLLoader.load(MessageBoxController.class.getResource("/MessageBox/MessageBoxFXML.fxml"));
Scene scene = new Scene(page);
messageBoxStage.setScene(scene);
messageBoxStage.setTitle("Message Box");
messageBoxStage.initOwner(this.myParent);
messageBoxStage.initModality(Modality.WINDOW_MODAL);
messageBoxStage.show();
} catch (Exception ex) {
System.out.println("Exception foundeth in showMessageBox");
ex.printStackTrace();
}
}
#Override
public void initialize(URL fxmlFileLocation, ResourceBundle arg1) {
txtMessage.setText("Howdy");
}
public void OnBtnYes(ActionEvent event) {
}
public void OnBtnNo(ActionEvent event) {
}
}
And finally, this is the FXML file I created in Scene Builder:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane2" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="172.0" prefWidth="524.0" xmlns:fx="http://javafx.com/fxml" fx:controller="MessageBox.MessageBoxController">
<children>
<VBox prefHeight="172.0" prefWidth="524.0" styleClass="vboxes" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<HBox alignment="CENTER" prefHeight="109.99990000000253" prefWidth="516.0" spacing="30.0">
<children>
<ImageView fx:id="imgMessage" fitHeight="110.0" fitWidth="146.66666666666666" pickOnBounds="true" preserveRatio="true" styleClass="null" />
<TextField fx:id="txtMessage" editable="false" prefHeight="47.0" prefWidth="325.0" />
</children>
<stylesheets>
<URL value="#MyCSS.css" />
</stylesheets>
</HBox>
<HBox alignment="CENTER" prefHeight="58.0" prefWidth="516.0" spacing="30.0">
<children>
<Button fx:id="btnYes" mnemonicParsing="false" onAction="#OnBtnYes" text="Button" />
<Button fx:id="btnNo" mnemonicParsing="false" onAction="#OnBtnNo" text="Button" />
</children>
</HBox>
</children>
<stylesheets>
<URL value="#MyCSS.css" />
</stylesheets>
</VBox>
</children>
<stylesheets>
<URL value="#MyCSS.css" />
</stylesheets>
</AnchorPane>
With this I can create a modal pop-up window, and I also want to create other child windows for displaying data in other ways using different controls. And, most importantly, I can use Scene Builder to create the layout.
What do you think? Is this a good way to do this until they add real support in Java 8 and JavaFX 8?
did you try wit the Group class? you can add diferent elements with fxml and controllers.
Group root= new Group();
AnchorPane frame=FXMLLoader.load(getClass().getResource("frame.fxml"));
AnchorPane content= FXMLLoader.load(getClass().getResource("principal.fxml"));
root.getChildren().add(window);
root.getChildren().add(frame);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
Related
I am learning JavaFX, and creating a scene that will be created and displayed to get user input. I created a VBox that holds different textfields and a button that I want to use to close the scene that was created and then store that information to use elsewhere. I can't seem to figure out how to add functionality to the button that will close the scene.
I've tried adding a EventHandler that will do it, but I can't seem to get the program to work.
part of Controller
public void drawNewClass(ActionEvent actionEvent) throws IOException {
ClassInstance classInstance = new ClassInstance();
TextField classDescription = new TextField("Enter Description");
TextField className = new TextField("Enter Class Name");
Button close = new Button("Submit");
close.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
}
});
TextField attributes = new TextField("Enter Variables (privacy type name, format)");
VBox secondaryLayout = new VBox();
secondaryLayout.getChildren().addAll(classDescription, className, attributes, close);
Scene secondScene = new Scene(secondaryLayout, 230, 100);
// New window (Stage)
Stage newWindow = new Stage();
newWindow.setTitle("Add Class");
newWindow.setScene(secondScene);
// Set position of second window, related to primary window.
newWindow.setX(canvas.getLayoutX() + 200);
newWindow.setY(canvas.getLayoutY() + 100);
newWindow.showAndWait();
.FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.layout.StackPane?>
<BorderPane xmlns="http://javafx.com/javafx/17"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.diagrambuilder.csc260w2022project2.diagramBuilderController"
prefHeight="500.0" prefWidth="500.0"
fx:id="root">
<top>
<HBox>
<Button text="Close" fx:id="closeButton" onAction="#closeApplication"/>
<Button text="New" onAction="#newCanvas"/>
<Button text="Save" onAction="#saveCanvas"/>
<Button text="Save As" onAction="#saveAsCanvas"/>
<Button text="Load" onAction="#loadCanvas"/>
<Button text="Export as Image" onAction="#exportCanvas"/>
</HBox>
</top>
<left>
<VBox>
<Button text="Draw New Class" fx:id="newClass" onAction="#drawNewClass"/>
<Button text="Add New Relationship" onAction="#drawNewRelationship"/>
</VBox>
</left>
<center>
<StackPane fx:id="stackPane" style="-fx-border-color: #000; -fx-border-width: 1px"
maxWidth="500" maxHeight="500">
<Canvas fx:id="canvas" width="${stackPane.width}" height="${stackPane.height}"/>
</StackPane>
</center>
</BorderPane>
Image of GUI
#Override
public void start(Stage stage) throws Exception {
BorderPane root = FXMLLoader.load(getClass().getClassLoader().getResource("mainView.fxml"));
When i run this it doesnt show the injected views
Im building a new application with JavaFX for the main page im using 3 views build by 3 fxml files
each view has its controller. for the main page i want to inject the three fxml files in a mainView.fxml via fx:include the mainView.fxml has also a controller how can i do that?
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>
<fx:root alignment="CENTER_LEFT" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="93.0" prefWidth="600.0" style="-fx-min-width: 800; -fx-min-height: 100; -fx-spacing: 30;" type="javafx.scene.layout.HBox" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label fx:id="title" alignment="CENTER" prefHeight="91.0" prefWidth="179.0" style="-fx-label-padding: 20; -fx-line-spacing: 20;" text="News" textFill="#00a4f2">
<font>
<Font name="Arial Black" size="36.0" />
</font>
<opaqueInsets>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</opaqueInsets></Label>
<Button fx:id="refresh" alignment="CENTER" contentDisplay="CENTER" mnemonicParsing="false" style="-fx-alignment: CENTER; -fx-background-color: #66a6ff;" text="Button" textAlignment="CENTER">
<opaqueInsets>
<Insets bottom="30.0" left="30.0" right="30.0" top="30.0" />
</opaqueInsets></Button>
<Button fx:id="stat" alignment="CENTER" mnemonicParsing="false" style="-fx-background-color: #feada6;" text="Button">
<opaqueInsets>
<Insets bottom="30.0" left="30.0" right="30.0" top="30.0" />
</opaqueInsets></Button>
</children>
</fx:root>
This is per example the topView.fxml
package ch.bfh.spacenews;
import java.io.IOException;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
public class TopBarController extends HBox {
#FXML
public Label title;
#FXML
public Button refresh;
#FXML
public Button stat;
TopBarController(){
FXMLLoader load = new FXMLLoader(getClass().getClassLoader().getResource("topView.fxml"));
load.setRoot(this);
load.setController(this);
try {
System.out.println("TopBarController");
load.load();
}catch(IOException e) {
e.printStackTrace();
}
}
}
This is the corresponding controller of the topView.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.*?>
<BorderPane fx:controller="ch.bfh.spacenews.mainController" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1">
<top>
<fx:include fx:id="topBar" source="topView.fxml"/>
</top>
<center>
<fx:include fx:id="article" source="sample.fxml"/>
</center>
<right>
<fx:include fx:id="seacrh" source="searchView.fxml"/>
</right>
</BorderPane>
This is where i want to inject the topView.fxml
package ch.bfh.spacenews;
import javafx.fxml.FXML;
public class mainController {
#FXML
TopBarController topBarController;
#FXML
ArticleController articleController;
#FXML
SearchController searchController;
#FXML
public void initialize() {
}
}
And this is the Controller of the mainView.fxml where i want to inject the topView.fxml
In the FXML custom component pattern the controller classes also serve as the wrapper for the view, by subclassing an appropriate Node subclass. This means you can just instantiate them directly in the FXML via the FXMLLoader. For example, the <TopBarController> element instructs the FXMLLoader to instantiate the TopBarController by calling its no-argument constructor. That constructor, as per your code, loads topView.fxml, etc. There is no need to use <fx:include>, which is an instruction for the FXMLLoader to load another FXML file, since you already have code in your TopBarController to do that.
So your main view FXML file should look like:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.*?>
<?import ch.bfh.spacenews.TopBarController?>
<!-- other imports... -->
<BorderPane fx:controller="ch.bfh.spacenews.MainController" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1">
<top>
<TopBarController fx:id="topBar" />
</top>
<center>
<ArticleController fx:id="article" />
</center>
<right>
<SearchController fx:id="search" />
</right>
</BorderPane>
And the corresponding controller is
package ch.bfh.spacenews;
import javafx.fxml.FXML;
public class MainController {
#FXML
TopBarController topBar;
#FXML
ArticleController article;
#FXML
SearchController search;
#FXML
public void initialize() {
}
}
Note that it's really critical to follow standard naming conventions here. The FXMLLoader explicitly relies on the case of an element to determine if it's referring to a class name or a property name.
Main class : Launch.java
public class Launch extends Application {
#Override
public void start(Stage stage) throws Exception {
Controller.show(stage);
}
public static void main(String[] args) {
launch(args);
}
}
You can add view from controller :
public class Controller {
public BorderPane mainPane; // fx:id of your pane
public static Stage current;
public static void show(Stage stage) {
this.current = stage;
Scene scene = null;
BorderPane pane = null;
try {
scene = new Scene(FXMLLoader.load(getClass().getResource("mainView.fxml")));
pane = (BorderPane) scene.lookup("#mainPane");
// Add .fxml view to mainView
pane.setCenter(FXMLLoader.load(getClass().getResource("sample.fxml")));
pane.setTop(FXMLLoader.load(getClass().getResource("topView.fxml")));
pane.setRight(FXMLLoader.load(getClass().getResource("searchView.fxml")));
} catch (IOException e) {
e.printStackTrace();
}
stage.setTitle("MainView");
stage.setScene(scene);
stage.show();
}
}
When I want to open a new window on a click event I need to load some
a value in a label, but the value is not refreshed.
I have tried load the new window an instantiate the label and set a value to be displayed, but nothing happens.
Bellow is the fxml and the code:
public class PetshopController implements Initializable {
#FXML
public ListView<String> ListaProgramari;
#FXML
public void completeTheAppointment(MouseEvent e) {
try {
String animalName = ListaProgramari.getSelectionModel().getSelectedItem();
DiagnosticController dc = new DiagnosticController();
dc.openDiagnosticWindow(animalName);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
public class DiagnosticController implements Initializable{
#FXML
private Label animalName = new Label();
#FXML
public TextField newDiagnostic;
#FXML
public Pane AnimalDetails;
#Override
public void initialize(URL location, ResourceBundle resources) {
}
public void openDiagnosticWindow(String animalLabel) {
try {
animalName = new Label();
animalName.setText(animalLabel);
BorderPane root = (BorderPane) FXMLLoader.load(getClass().getResource("/controller/Diagnostic.fxml"));
Scene scene = new Scene(root, 600, 400);
scene.getStylesheets().add(getClass().getResource("/controller/application.css").toExternalForm());
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
Bellow is the FXML. This contains all the items from the new window which is opened the problem is at the label: "animalName":
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane prefHeight="303.0" prefWidth="452.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.DiagnosticController">
<top>
<MenuBar prefHeight="0.0" prefWidth="480.0" BorderPane.alignment="CENTER" />
</top>
<center>
<SplitPane dividerPositions="0.6445182724252492" orientation="VERTICAL" prefHeight="200.0" prefWidth="160.0" BorderPane.alignment="CENTER">
<items>
<AnchorPane fx:id="AnimalDetails" minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<TextField fx:id="newDiagnostic" layoutX="190.0" layoutY="131.0" />
<Label layoutX="48.0" layoutY="135.0" prefHeight="17.0" prefWidth="120.0" text="Adauga Diagnostic" />
<Label layoutX="42.0" layoutY="75.0" prefHeight="17.0" prefWidth="120.0" text="Numele Animalului:" />
<Label fx:id="animalName" layoutX="189.0" layoutY="75.0" prefHeight="17.0" prefWidth="145.0" text="empty" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="49.0" prefWidth="450.0">
<children>
<Button fx:id="completeConsultation" layoutX="172.0" layoutY="46.0" mnemonicParsing="false" onMouseClicked="#completeConsultation" prefHeight="36.0" prefWidth="79.0" text="Complete" />
</children>
</AnchorPane>
</items>
</SplitPane>
</center>
</BorderPane>
By doing #FXML private Label animalName = new Label(); you construct a new label instead of the one already created by the fxml. In fact you do it twice : this Label is initialized again in openDiagnosticWindow by animalName = new Label();
Just use #FXML public Label animalName;
Another problem is in those lines:
DiagnosticController dc = new DiagnosticController();
dc.openDiagnosticWindow(animalName);
dc is a reference to an instance of DiagnosticController but not the instance used by the fxml.
To get a reference of the controller used by the fxml you need to get it from the loader:
FXMLLoader loader = new FXMLLoader();
BorderPane root = loader.load(getClass().getResource("/controller/Diagnostic.fxml") .openStream());
DiagnosticController dc = (DiagnosticController)loader.getController();
which means loading Diagnostic.fxml should be done by PetshopController and not by DiagnosticController.
For more help please post mcve. Important information is missing (like what is the name of the fxml file posted ? What invokes PetshopController ? and more) which forces us to guess.
I have a UI I have made using Netbeans and Scene builder using Java FX / FXML. I have never had a problem with it before but for this project for some reason in Scene building my UI would look one way, even the windows preview would look identical to it, but when running it in netbeans it would look messed up. In netbeans, all I am doing is loading and calling it. I have no idea on what the issue can be and nothing is helping on Google.
working screen shot on scene builder and editing and netbeans
http://imgur.com/laZYjck
compile and run
http://imgur.com/NOMVBZe
main.java
public class main
{
public static void main(String[] args)
{
View view = new View();
view.launch();
}
}
view.java
public class View extends Application
{
#FXML
private Button login;
#Override
public void start(Stage primaryStage)
{
Parent login_page = null;
try {
login_page = FXMLLoader.load(getClass().getResource("FXMLViewLogin.fxml"));
}
catch (IOException ex)
{
}
primaryStage.setTitle("Welcome!");
primaryStage.setScene(new Scene(login_page));
primaryStage.show();
}
#FXML
private void login(ActionEvent event)
{}
}
FXMLViewLogin.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="520.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.ViewControllerLogin">
<children>
<Button fx:id="button1" defaultButton="true" layoutX="74.0" layoutY="296.0" text="Sign Up" />
<Label layoutX="198.0" layoutY="153.0" text="Password" />
<Label layoutX="187.0" layoutY="35.0" text="User Name" />
<TextField fx:id="password" layoutX="74.0" layoutY="204.0" />
<TextField fx:id="username" layoutX="74.0" layoutY="80.0" />
<Button fx:id="button" layoutX="323.0" layoutY="296.0" onAction="#login" text="Log In" />
</children>
</Pane>
Say I have a window, with a button. And everytime that button is pressed I want the current window to be disposed and a new one to be shown.
In Swing, that was easy, but I can't find the syntax for it in JavaFx?
So in this case, with my example code, how do I do this?
public class Main extends Application {
Parent root;
Scene scene;
#Override
public void start(Stage primaryStage) throws Exception {
root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.sizeToScene();
primaryStage.setResizable(false);
primaryStage.show();
root.setOnMouseClicked((MouseEvent mouseEvent) -> {
if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) {
if (mouseEvent.getClickCount() == 2) {
Stage stage = new Stage();
stage.setScene(primaryStage.getScene());
stage.show();
primaryStage.close();
}
}
});
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Trying to create a new window: http://sv.tinypic.com/r/1jkq4w/8
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 java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>
<VBox prefHeight="430.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="blackjack.FXMLDocumentController">
<children>
<AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
<children>
<Label fx:id="labelPlayerTotal" layoutX="510.0" layoutY="335.0" prefHeight="15.0" prefWidth="121.0" text="Playertotal:" />
<Label fx:id="labelDealerTotal" layoutX="510.0" layoutY="359.0" prefHeight="15.0" prefWidth="112.0" text="Dealertotal:" />
<TextArea fx:id="dealerArea" layoutY="29.0" prefHeight="159.0" prefWidth="503.0" />
<TextArea fx:id="playerArea" layoutY="244.0" prefHeight="159.0" prefWidth="503.0" />
<Button fx:id="stayButton" layoutX="512.0" layoutY="173.0" mnemonicParsing="false" onAction="#drawCardForDealer" prefHeight="25.0" prefWidth="72.0" text="STAY" />
<Button fx:id="hitButton" layoutX="512.0" layoutY="130.0" mnemonicParsing="false" onAction="#drawCardForPlayer" prefHeight="25.0" prefWidth="72.0" text="HIT" />
<Label fx:id="labelWinner" layoutX="104.0" layoutY="208.0" prefHeight="15.0" prefWidth="296.0" />
<MenuBar fx:id="helpBar">
<menus>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" onAction="#aboutApplication" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</children>
</AnchorPane>
</children>
</VBox>
You can just hide (i.e. dispose) the window by calling hide() or close() on it.
And you can just show a new window with exactly the same code you used before:
primaryStage.close();
Stage stage = new Stage();
root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
scene = new Scene(root);
stage.setScene(scene);
stage.sizeToScene();
stage.setResizable(false);
stage.show();
But this seems like way too much for what you want to achieve. Why not just replace the root of the existing scene, and use the existing stage?
try {
scene.setRoot(FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")));
} catch (Exception exc) {
exc.printStackTrace();
throw new RuntimeException(exc);
}
Note though that (either way you do this) you have now replaced the root of the scene; the new root does not have the same mouse handler associated with it. If you use the second method, you can just put the handler on the scene (which doesn't change) instead. Or, perhaps better, is that you can define the listener in the FXML and controller:
<VBox prefHeight="430.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="blackjack.FXMLDocumentController"
fx:id="root"
onMouseClicked="reload">
<!-- ... -->
</VBox>
And the controller:
package blackjack ;
public class FXMLDocumentController {
// ...
#FXML
private VBox root ;
// ...
#FXML
private void reload() throws Exception {
// Really, it would be way better here to reset the whole UI to its initial
// state, instead of reloading it from scratch. This should work as a
// quick hack though.
Scene scene = root.getScene();
scene.setRoot(FXMLLoader.load(Main.class.getResource("FXMLDocument.fmxl")));
}
// ...
}