More efficient way to add Panes to an HBox? - javafx

I am working on a project that is going to involve adding panes (or hboxes if that works better) to a very wide HBox. I set it up in an fxml pretty easily but I'm already going to have to make a few of these and I would love to avoid all the copying pasting. So I figured I should try and use a for loop to populate the HBox but for whatever reason it is not working. I'm in the very beginning steps so my code is very simple and straight forward.
This is the basic sample I'm trying to recreate
<HBox>
<children>
<Pane fx:id="pane0" prefHeight="200.0" prefWidth="200.0">
<children>
<Button layoutX="71.0" layoutY="95.0" mnemonicParsing="false" text="Button" />
</children></Pane>
<Pane fx:id="pane1" prefHeight="200.0" prefWidth="200.0">
<children>
<Button layoutX="71.0" layoutY="95.0" mnemonicParsing="false" text="Button" />
</children></Pane>
<Pane fx:id="pane2" prefHeight="200.0" prefWidth="200.0">
<children>
<Button layoutX="71.0" layoutY="95.0" mnemonicParsing="false" text="Button" />
</children></Pane>
</children>
</HBox>
So to create it dynamically (and for clarity) I created a class for the HBox and the panes. For now each pane just has a button but they will undergo further customization once this code works. Same with the HBox.
Here's the HBox
public class HBoxTestClass {
#FXML
HBox hBox = new HBox();
public HBoxTestClass(){
}
#FXML
public void initialize(){
populateHBox();
}
private void populateHBox(){
for (int i = 0; i < 3; i++){
hBox.getChildren().add(new TestPane());
hBox.setSpacing(10);
}
}
}
its fxml
<HBox fx:id="hBox" xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="sample.HBoxTestClass"
prefHeight="400.0" prefWidth="600.0">
</HBox>
The pane class and its fxml
public class TestPane extends Pane{
#FXML Pane testPane = new Pane();
#FXML Button button = new Button();
public TestPane(){
}
#FXML
private void initialize(){
button.setText("Click Me!");
}
}
<Pane fx:id="testPane" xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="sample.TestPane"
prefHeight="200.0" prefWidth="200.0">
<children>
<Button fx:id="button" layoutX="71.0" layoutY="95.0" mnemonicParsing="false" text="Button" />
</children>
</Pane>
So my very simple code above just produces a blank, wide screen with no panels in it. I put a console print in the constructor of my TestPane class so I know it's getting called but still nothing appears. Any advice? Thanks

I am not sure what you are doing in your code incorrectly. Code like #FXML Pane testPane = new Pane(); should look like #FXML Pane testPane;. I am not sure how code like #FXML
public void initialize(){ is being called in your program. Try to follow this MCVE.
Main
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* #author blj0011
*/
public class JavaFXApplication363 extends Application
{
#Override
public void start(Stage stage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
Controller
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
/**
*
* #author blj0011
*/
public class FXMLDocumentController implements Initializable
{
#FXML
private HBox hBox;
#Override
public void initialize(URL url, ResourceBundle rb)
{
// TODO
for (int i = 0; i < 5; i++) {
StackPane stackPane = new StackPane(new Label("Label: " + i));
hBox.getChildren().add(stackPane);
}
}
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.HBox?>
<HBox fx:id="hBox" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication363.FXMLDocumentController" />

Related

JavaFX:How to slide a pane properly when stage with shadow effect?

I have seen this question on "JavaFX: how to create slide in animation effect for a pane (inside a transparent stage)".
And I don't understand that partial code on the last comment posted by John Astralidis. It seems to solve my problem. I would like to slide a pane with a shadow stage. Now my problem is the slide animation play out of my visual root pane bound, it just play with stage (or actual root pane) bound.
My visual root pane is a child of actual root pane, I set the actual root pane with padding and Corlor.TRANSPARENT to implement my visual root pane shadow effect.
here is the code I had modified from John Astralidis and Felipe Guizar Diaz.
the start code:
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
stage.initStyle(StageStyle.TRANSPARENT);
Scene scene = new Scene(root);//
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
the FXMLDocument.fxml:
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" fx:id="anchorPane" prefWidth="500" prefHeight="500" style="-fx-background-color: transparent;"
fx:controller="leftslidemenusample.FXMLDocumentController">
<children>
<ToolBar AnchorPane.topAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" minHeight="56.0" >
<Button text="menu" fx:id="menu" />
</ToolBar>
<StackPane fx:id="mainContent" style="-fx-background-color:rgba(0,0,0,0.30)" AnchorPane.bottomAnchor="0.0" AnchorPane.topAnchor="56.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" >
<children>
</children>
</StackPane>
<AnchorPane fx:id="navList" style="-fx-background-color:white" AnchorPane.topAnchor="56.0" AnchorPane.bottomAnchor="0.0" prefWidth="180.0" translateX="-180" >
<children>
<Label text="left side menu"/>
</children>
</AnchorPane>
</children>
and the controller FXMLDocumentController.java
public class FXMLDocumentController implements Initializable {
#FXML
private AnchorPane anchorPane;
#FXML
private Button menu;
#FXML
private AnchorPane navList;
private double shadowSize = 15;
#Override
public void initialize(URL url, ResourceBundle rb) {
Rectangle rectangle = new Rectangle(500,500);
anchorPane.setClip(rectangle);
anchorPane.getChildren().add(setupShadowPane());
prepareSlideMenuAnimation();
}
private Pane setupShadowPane() {
Pane shadowPane = new Pane();
shadowPane.setPrefHeight(500);
shadowPane.setPrefWidth(500);
shadowPane.setStyle(
"-fx-background-color: RED;" +
"-fx-effect: dropshadow(gaussian, black, " + 20 + ", 0, 0, 0);" +
"-fx-background-insets: " + shadowSize + ";"
);
Rectangle innerBounds = new Rectangle();
Rectangle outerBounds = new Rectangle();
shadowPane.layoutBoundsProperty().addListener((observable, oldBounds, newBounds) -> {
System.out.println(newBounds.getWidth());
innerBounds.relocate(newBounds.getMinX() + shadowSize, newBounds.getMinY() + shadowSize);
innerBounds.setWidth(newBounds.getWidth() - shadowSize * 2);
innerBounds.setHeight(newBounds.getHeight() - shadowSize * 2);
outerBounds.setWidth(newBounds.getWidth());
outerBounds.setHeight(newBounds.getHeight());
Shape clip = Shape.subtract(outerBounds, innerBounds);
shadowPane.setClip(clip);
});
return shadowPane;
}
private void prepareSlideMenuAnimation() {
TranslateTransition openNav=new TranslateTransition(new Duration(350), navList);
openNav.setToX(0);
TranslateTransition closeNav=new TranslateTransition(new Duration(350), navList);
menu.setOnAction((ActionEvent evt)->{
if(navList.getTranslateX()!=0){
openNav.play();
}else{
closeNav.setToX(-(navList.getWidth()));
closeNav.play();
}
});
}
}
Finally,I get it done.I think John Astralidis's answer is wrong.But thanks.The keys are:
Use a StackPane as a actual root pane,then add visual root pane and shadow pane to actual root pane as StackPane's children.Visual root pane is the need to add shadow effect pane.
Set visual root pane with chip that corps its outer layout to show shadow pane's shadow effect;
Set shadow pane with chip that corps its inner layout to show visual root pane's content.
Wishing my answer with my bad English can help somebody.I post my entire demo code below:
The starter class--FXApplication.java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class FXApplication extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
stage.initStyle(StageStyle.TRANSPARENT);
Scene scene = new Scene(root);//
scene.setFill(Color.TRANSPARENT);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The layout FXML file--FXMLDocument.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.*?>
<StackPane xmlns:fx="http://javafx.com/fxml/1" fx:id="pane" prefWidth="500" prefHeight="500" style="-fx-background-color: transparent;"
fx:controller="FXMLDocumentController">
<children>
<AnchorPane fx:id="anchorPane" prefWidth="500" prefHeight="500" style="-fx-background-color: WHITE;">
<children>
<ToolBar AnchorPane.topAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" minHeight="56.0" >
<Button text="menu" fx:id="menu" />
</ToolBar>
<StackPane fx:id="mainContent" style="-fx-background-color:rgba(0,0,0,0.30)" AnchorPane.bottomAnchor="0.0" AnchorPane.topAnchor="56.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" >
<children>
</children>
</StackPane>
<AnchorPane fx:id="navList" style="-fx-background-color:white" AnchorPane.topAnchor="56.0" AnchorPane.bottomAnchor="0.0" prefWidth="180.0" translateX="-180" >
<children>
<Label text="left side menu"/>
</children>
</AnchorPane>
</children>
</AnchorPane>
</children>
The controller class--FXMLDocumentController.java
import javafx.animation.TranslateTransition;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.util.Duration;
import java.net.URL;
import java.util.ResourceBundle;
public class FXMLDocumentController implements Initializable {
#FXML
private AnchorPane anchorPane;
#FXML
private StackPane pane;
#FXML
private Button menu;
#FXML
private AnchorPane navList;
private double shadowSize = 10;
#Override
public void initialize(URL url, ResourceBundle rb) {
Rectangle rectangle = new Rectangle(480,480);
rectangle.relocate(10,10);
anchorPane.setClip(rectangle);
pane.getChildren().add(setupShadowPane());
prepareSlideMenuAnimation();
}
private Pane setupShadowPane() {
Pane shadowPane = new Pane();
shadowPane.setPrefHeight(500);
shadowPane.setPrefWidth(500);
shadowPane.setStyle(
"-fx-background-color: RED;" +
"-fx-effect: dropshadow(gaussian, black, " + 20 + ", 0, 0, 0);" +
"-fx-background-insets: " + shadowSize + ";"
);
Rectangle innerBounds = new Rectangle();
Rectangle outerBounds = new Rectangle();
shadowPane.layoutBoundsProperty().addListener((observable, oldBounds, newBounds) -> {
innerBounds.relocate(newBounds.getMinX() + shadowSize, newBounds.getMinY() + shadowSize);
innerBounds.setWidth(newBounds.getWidth() - shadowSize * 2);
innerBounds.setHeight(newBounds.getHeight() - shadowSize * 2);
outerBounds.setWidth(newBounds.getWidth());
outerBounds.setHeight(newBounds.getHeight());
Shape clip = Shape.subtract(outerBounds, innerBounds);
shadowPane.setClip(clip);
});
return shadowPane;
}
private void prepareSlideMenuAnimation() {
TranslateTransition openNav=new TranslateTransition(new Duration(350), navList);
openNav.setToX(0 + shadowSize);
TranslateTransition closeNav=new TranslateTransition(new Duration(350), navList);
menu.setOnAction((ActionEvent evt)->{
if(navList.getTranslateX()!=0+shadowSize){
openNav.play();
}else{
closeNav.setToX(-(navList.getWidth())+shadowSize);
closeNav.play();
}
});
}
}
And the image below:

How to update a scene without refreshing the scene after receiving data from another class/controller

I'm working on a personal weather project that contains Main.fxml, MainController.java, EnterCityDocument.fxml, and EnterCityDocumentController.java.
Main.fxml: contains a border-pane and at its center it has a ListView that displays names of cities. It also has a "Add" Button to open a new modal window(EnterCityDocument.fxml) to add a city to its ListView.
EnterCityDocument.fxml: has a listView that contains names of cities and a "Select" button to select a city the user wants to display in Main.fxml. When the user clicks the "Select" Button, the modal window (EnterCityDocument.fxml) closes and the Main.fxml continues to run.
MainController.java is the parent class of EnterCityDocumentController.java.
I've been looking for passing data from the child class(EnterCityDocumentController.java) to the parent class (MainController.java) and found a way to do it, but all of the methods i've found require to refresh the MainController.java class whenever the user selects a city in EnterCityDocumentController.java.
Is there a way to update the Main.fxml without refreshing the main scene when a new city is added into the listView of the Main.fxml?
Hope my question is clear enough. If you need a further explanation/code, please let me know!
All you need to do is arrange for the new city from the dialog to be added to the list view's backing list. Since you are using showAndWait() this is very easy: just define a method in the EnterCityDocumentController class that returns the new city, and call it after showAndWait():
public class EnterCityDocumentController {
#FXML
private TextField cityNameField ;
// other fields, etc...
#FXML
private void okButtonPressed() {
// just close the window:
cityNameField.getScene().getWindow().hide();
}
public City getUserCity() {
return new City(cityNameField.getText());
}
}
Then in the main controller:
public class MainController {
#FXML
private ListView<City> listView ;
// handler method:
#FXML
public void addNewCity() throws IOException {
FXMLLoader loader = new FXMLLoader(EnterCityDocumentController.class.getResource("EnterCityDocument.fxml"));
Scene scene = new Scene(loader.load());
Stage stage = new Stage();
stage.setScene(scene);
stage.showAndWait();
listView.getItems().add(controller.getUserCity());
}
}
Here is a SSCCE:
app/Main.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.MainController">
<center>
<ListView fx:id="listView" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</center>
<bottom>
<Button mnemonicParsing="false" onAction="#addNewElement" text="Add..." BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets bottom="2.0" left="2.0" right="2.0" top="2.0" />
</BorderPane.margin>
</Button>
</bottom>
</BorderPane>
app/AddNewElement.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane hgap="5.0" vgap="5.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.AddNewElementController">
<rowConstraints>
<RowConstraints />
<RowConstraints />
</rowConstraints>
<columnConstraints>
<ColumnConstraints halignment="CENTER" />
<ColumnConstraints halignment="CENTER" />
</columnConstraints>
<children>
<TextField fx:id="textField" GridPane.columnSpan="2" />
<Button defaultButton="true" mnemonicParsing="false" onAction="#ok" text="OK" GridPane.rowIndex="1" />
<Button cancelButton="true" mnemonicParsing="false" onAction="#cancel" text="Cancel" GridPane.columnIndex="1" GridPane.rowIndex="1" />
</children>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</GridPane>
app/AddNewElementController.java:
package app;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
public class AddNewElementController {
#FXML
private TextField textField ;
private boolean approved ;
public boolean isApproved() {
return approved ;
}
public String getUserText() {
return isApproved() ? textField.getText() : null ;
}
#FXML
private void cancel() {
approved = false ;
hide();
}
#FXML
private void ok() {
approved = true ;
hide();
}
private void hide() {
textField.getScene().getWindow().hide();
}
}
app/MainController.java:
package app;
import java.io.IOException;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class MainController {
#FXML
private ListView<String> listView ;
#FXML
private void addNewElement() throws IOException {
FXMLLoader loader = new FXMLLoader(AddNewElementController.class.getResource("AddNewElement.fxml"));
Parent root = loader.load();
AddNewElementController controller = loader.getController();
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.initOwner(listView.getScene().getWindow());
stage.setScene(scene);
stage.showAndWait();
if (controller.isApproved()) {
listView.getItems().add(controller.getUserText());
}
}
}
app/Main.java:
package app;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws IOException {
FXMLLoader loader = new FXMLLoader(MainController.class.getResource("Main.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Another, very slightly different approach, is to pass the listview's backing list to the second controller. Then the second controller can add the item directly to the list (and the list view will automatically update, as before). This approach can be used even if you are not using showAndWait() (it doesn't rely on the code blocking until the user dismisses the window).
package app;
import java.util.List;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
public class AddNewElementController {
#FXML
private TextField textField ;
private List<String> itemList ;
public void setItemList(List<String> itemList) {
this.itemList = itemList ;
}
#FXML
private void cancel() {
hide();
}
#FXML
private void ok() {
itemList.add(textField.getText());
hide();
}
private void hide() {
textField.getScene().getWindow().hide();
}
}
and
package app;
import java.io.IOException;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class MainController {
#FXML
private ListView<String> listView ;
#FXML
private void addNewElement() throws IOException {
FXMLLoader loader = new FXMLLoader(AddNewElementController.class.getResource("AddNewElement.fxml"));
Parent root = loader.load();
AddNewElementController controller = loader.getController();
controller.setItemList(listView.getItems());
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.initOwner(listView.getScene().getWindow());
stage.setScene(scene);
stage.showAndWait();
}
}

Button won't work javafx

I'm a total beginner in programming. I've been trying to create a simple program that gets two values from the user (two grades from 0 to 10) and returns the average between them. I've done it with Swing and JOptionPane but using javafx I got nothing. Here goes the code.
package fxcalcmedia;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class FXCalcMedia extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root =
FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
stage.setTitle("CALCULADORA DE MÉDIA");
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
package fxcalcmedia;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.text.Text;
public class FXMLDocumentController implements Initializable {
#FXML
private Label lblMedia;
private Button btnCalc;
private Text txtMedia;
private Text txtNota1;
private Text txtNota2;
#FXML
private void clicouBotao(ActionEvent event) {
double nota1 = Double.parseDouble(txtNota1.getText());
double nota2 = Double.parseDouble(txtNota2.getText());
double media = (nota1+nota2)/2;
txtMedia.setText(Double.toString(media));
}
#Override
public void initialize(URL url, ResourceBundle rb) {
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="155.0" prefWidth="303.0"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="fxcalcmedia.FXMLDocumentController">
<children>
<Label fx:id="lblMedia" layoutX="30.0" layoutY="119.0"
text="Média" />
<Label layoutX="28.0" layoutY="33.0" text="Nota 1" />
<Label layoutX="28.0" layoutY="76.0" text="Nota 2" />
<Button fx:id="btnCalc" layoutX="212.0" layoutY="71.0"
mnemonicParsing="false" onAction="#clicouBotao" text="Calcular"
/>
<TextField fx:id="txtNota1" layoutX="90.0" layoutY="28.0"
prefHeight="25.0" prefWidth="78.0" />
<TextField fx:id="txtNota2" layoutX="90.0" layoutY="71.0"
prefHeight="25.0" prefWidth="78.0" />
<TextField fx:id="txtMedia" layoutX="90.0" layoutY="114.0"
onAction="#clicouBotao" prefHeight="25.0" prefWidth="78.0" />
</children>
</AnchorPane>
So... the window appears, I insert the grades but when I click on the button to get the average grade, nothing happens. I've tried a lot of things, I've searched a lot... I hope somebody can help. Thanks again.
You need to place #FXML annotation on each Node that have fx:id="" in fxml file. It is necessary for FXMLLoader to create correct instances and inject them to controller.
Another problem is components type in fxml file. In fxml you use TextField
<TextField fx:id="txtNota1" layoutX="90.0" layoutY="28.0"
prefHeight="25.0" prefWidth="78.0" />
<TextField fx:id="txtNota2" layoutX="90.0" layoutY="71.0"
prefHeight="25.0" prefWidth="78.0" />
<TextField fx:id="txtMedia" layoutX="90.0" layoutY="114.0"
onAction="#clicouBotao" prefHeight="25.0" prefWidth="78.0" />
but in cotntroller you use Text:
private Text txtMedia;
private Text txtNota1;
private Text txtNota2;
Solution:
#FXML
private Label lblMedia;
#FXML
private Button btnCalc;
#FXML
private TextField txtMedia;
#FXML
private TextField txtNota1;
#FXML
private TextField txtNota2;
As an extension to #MBec's answer. A very nice tool for creating GUIs for javaFX is JavaFX Scene Builder. It provides you with drag & drop functionality and also gives templates for each fxml file's Controller class.

JavaFX FXML Show Updated TableView

I'm having issues showing new additions to a class in a TableView for my JavaFX/FXML program. I've looked at countless tutorials but the missing piece still escapes me.
I had some errors that got fixed here: JavaFX Adding Rows to TableView on Different Page
And then found a new tutorial to sort of follow that explained things a bit better than the first one I was following (their app is a bit different than what I have to do) here: http://code.makery.ch/library/javafx-8-tutorial/part2/
I have an output printing the name field out to the console just to make sure it is pulling the right values from the add form. I also changed the way I navigated between Add Part and the Main window (nothing else works right now). I feel so silly asking this, but Java is the one language I haven't been able to wrap my head around.
Any ideas on why it isn't updating are greatly appreciated.
IMS.java
package ims;
import java.io.IOException;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
/**
*
* #author chelseacamper
*/
public class IMS extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
private ObservableList<Part> partData = FXCollections.observableArrayList();
public IMS() {
partData.add(new Part("Part A", 3, 4.00, 1, 5));
partData.add(new Part("Part B", 2, 14.00, 1, 15));
}
public ObservableList<Part> getPartData(){
return partData;
}
#Override
public void start(Stage stage) throws Exception {
// Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
Parent root = (Parent) loader.load();
FXMLDocumentController ctrl = loader.getController();
ctrl.setMainApp(this);
Scene scene = new Scene(root);
scene.getStylesheets().add("style.css");
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
FXMLDocumentController.java
package ims;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
/**
*
* #author chelseacamper
*/
public class FXMLDocumentController implements Initializable {
#FXML
private Label label;
#FXML
private TableView<Part> partTable;
#FXML
private TableColumn<Part, Integer> partIDColumn;
#FXML
private TableColumn<Part, String> nameColumn;
#FXML
private TableColumn<Part, Integer> inventoryColumn;
#FXML
private TableColumn<Part, Double> priceColumn;
private IMS mainApp;
public FXMLDocumentController(){
}
#FXML
private void addPart(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("addPart.fxml"));
Parent add_part_parent = (Parent) loader.load();
Stage stage = new Stage();
stage.setScene(new Scene(add_part_parent));
stage.show();
}
#FXML
private void modifyPart(ActionEvent event) throws IOException {
Parent modify_part_parent = FXMLLoader.load(getClass().getResource("modifyPart.fxml"));
Scene modify_part_scene = new Scene(modify_part_parent);
modify_part_scene.getStylesheets().add("style.css");
Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
app_stage.setScene(modify_part_scene);
app_stage.show();
}
#FXML
private void addProduct(ActionEvent event) throws IOException {
Parent add_product_parent = FXMLLoader.load(getClass().getResource("addProduct.fxml"));
Scene add_product_scene = new Scene(add_product_parent);
add_product_scene.getStylesheets().add("style.css");
Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
app_stage.setScene(add_product_scene);
app_stage.show();
}
#FXML
private void modifyProduct(ActionEvent event) throws IOException {
Parent modify_product_parent = FXMLLoader.load(getClass().getResource("modifyProduct.fxml"));
Scene modify_product_scene = new Scene(modify_product_parent);
modify_product_scene.getStylesheets().add("style.css");
Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
app_stage.setScene(modify_product_scene);
app_stage.show();
}
#FXML
private void closeProgram(ActionEvent event) throws IOException {
Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
app_stage.close();
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// nameColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
// inventoryColumn.setCellValueFactory(cellData -> cellData.getValue().instockProperty().asObject());
// priceColumn.setCellValueFactory(cellData -> cellData.getValue().priceProperty().asObject());
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
inventoryColumn.setCellValueFactory(new PropertyValueFactory<>("instock"));
priceColumn.setCellValueFactory(new PropertyValueFactory<>("price"));
}
public void setMainApp(IMS mainApp) {
this.mainApp = mainApp;
// Add observable list data to the table
partTable.setItems(mainApp.getPartData());
}
#FXML
private void handleDeletePart(){
int selectedIndex = partTable.getSelectionModel().getSelectedIndex();
if (selectedIndex >= 0){
partTable.getItems().remove(selectedIndex);
} else {
Alert alert = new Alert(AlertType.WARNING);
alert.setTitle("No Part Selected");
alert.setHeaderText("No Part Selected");
alert.setContentText("Please select the part you would like to delete.");
alert.showAndWait();
}
}
}
FXMLDocument.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.control.cell.*?>
<?import javafx.collections.*?>
<?import fxmltableview.*?>
<?import ims.Part?>
<?import ims.Inhouse?>
<?import ims.Outsourced?>
<BorderPane id="main" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ims.FXMLDocumentController" >
<top>
<Label fx:id="mainTitle" text="Inventory Management System" />
</top>
<center>
<HBox fx:id="holding">
<children>
<VBox styleClass="contentBox">
<children>
<HBox styleClass="topBox">
<HBox styleClass="subHeading">
<Label text="Parts" />
</HBox>
<HBox styleClass="searchBox">
<Button text="Search" />
<TextField />
</HBox>
</HBox>
<TableView fx:id="partTable" styleClass="dataTable">
<columns>
<TableColumn fx:id="partIDColumn" text="Part ID" />
<TableColumn fx:id="nameColumn" text="Part Name" />
<TableColumn fx:id="inventoryColumn" text="Inventory Level" />
<TableColumn fx:id="priceColumn" text="Price/Cost per Unit" />
</columns>
</TableView>
<HBox styleClass="modificationButtons">
<children>
<Button onAction="#addPart" text="Add" />
<Button onAction="#modifyPart" text="Modify" />
<Button text="Delete" />
</children>
</HBox>
</children>
</VBox>
<VBox styleClass="contentBox">
<children>
<HBox styleClass="topBox">
<HBox styleClass="subHeading">
<Label text="Products" />
</HBox>
<HBox styleClass="searchBox">
<Button text="Search" />
<TextField />
</HBox>
</HBox>
<TableView fx:id="productTable" styleClass="dataTable">
<columns>
<TableColumn text="Part ID" />
<TableColumn text="Part Name" />
<TableColumn text="Inventory Level" />
<TableColumn text="Price/Cost per Unit" />
</columns>
</TableView>
<HBox styleClass="modificationButtons">
<children>
<Button onAction="#addProduct" text="Add" />
<Button onAction="#modifyProduct" text="Modify" />
<Button text="Delete" />
</children>
</HBox>
</children>
</VBox>
</children>
</HBox>
</center>
<bottom>
<HBox fx:id="exitButton">
<children>
<Button onAction="#closeProgram" text="Exit" />
</children>
</HBox>
</bottom>
</BorderPane>
Part.java
package ims;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
/**
*
* #author chelseacamper
*/
public class Part {
private final SimpleStringProperty name;
private final SimpleIntegerProperty instock;
private final SimpleDoubleProperty price;
private final SimpleIntegerProperty min;
private final SimpleIntegerProperty max;
public Part(){
this("", 0, 0.00, 0, 0);
}
public Part(String name, int instock, double price, int min, int max) {
this.name = new SimpleStringProperty(name);
this.instock = new SimpleIntegerProperty(instock);
this.price = new SimpleDoubleProperty(price);
this.min = new SimpleIntegerProperty(min);
this.max = new SimpleIntegerProperty(max);
}
public String getName() {
return name.get();
}
public void setName(String name) {
this.name.set(name);
}
public StringProperty nameProperty() {
return name;
}
public Double getPrice() {
return price.get();
}
public void setPrice(Double price) {
this.price.set(price);
}
public DoubleProperty priceProperty(){
return price;
}
public int getInstock() {
return instock.get();
}
public void setInstock(int instock) {
this.instock.set(instock);
}
public IntegerProperty instockProperty(){
return instock;
}
public int getMin() {
return min.get();
}
public void setMin(int min) {
this.min.set(min);
}
public IntegerProperty minProperty(){
return min;
}
public int getMax() {
return max.get();
}
public void setMax(int max) {
this.max.set(max);
}
public IntegerProperty maxProperty(){
return max;
}
}
addPartController.java
package ims;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleButton;
import javafx.stage.Stage;
/**
* FXML Controller class
*
* #author chelseacamper
*/
public class AddPartController implements Initializable {
#FXML
ToggleButton inhouse;
#FXML
ToggleButton outsourced;
#FXML
Label inhouseLabel;
#FXML
Label outsourcedLabel;
#FXML
TextField inhouseTextField;
#FXML
TextField outsourcedTextField;
#FXML
private TableView<Part> partTable;
#FXML
private TextField partNameField;
#FXML
private TextField partInstockField;
#FXML
private TextField partPriceField;
#FXML
private TextField partMaxField;
#FXML
private TextField partMinField;
#FXML
private Button cancel;
#FXML
private Button save;
private Part part = new Part();
// private ObservableList<Part> partData = FXCollections.observableArrayList();
private ObservableList<Part> tableItems;
/**
* Initializes the controller class.
* #param url
* #param rb
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
inhouseLabel.visibleProperty().bind( inhouse.selectedProperty() );
outsourcedLabel.visibleProperty().bind( outsourced.selectedProperty() );
inhouseTextField.visibleProperty().bind( inhouse.selectedProperty() );
outsourcedTextField.visibleProperty().bind( outsourced.selectedProperty() );
inhouseLabel.managedProperty().bind( inhouse.selectedProperty() );
outsourcedLabel.managedProperty().bind( outsourced.selectedProperty() );
inhouseTextField.managedProperty().bind( inhouse.selectedProperty() );
outsourcedTextField.managedProperty().bind( outsourced.selectedProperty() );
}
#FXML
public void addInhouse(ActionEvent event) throws IOException{
// Part tempPart = new Part(partNameField.getText(),
// Integer.parseInt(partInstockField.getText()),
// Double.parseDouble(partPriceField.getText()),
// Integer.parseInt(partMaxField.getText()),
// Integer.parseInt(partMinField.getText()));
// tableItems.add(new Part(partNameField.getText(),
// Integer.parseInt(partInstockField.getText()),
// Double.parseDouble(partPriceField.getText()),
// Integer.parseInt(partMaxField.getText()),
// Integer.parseInt(partMinField.getText())
// ));
part
.setName(partNameField.getText());
part.setPrice(Double.parseDouble(partPriceField.getText()));
part.setInstock(Integer.parseInt(partInstockField.getText()));
part.setMin(Integer.parseInt(partMinField.getText()));
part.setMax(Integer.parseInt(partMaxField.getText()));
System.out.println(partNameField.getText());
Stage stage = (Stage) cancel.getScene().getWindow();
stage.close();
}
#FXML
private void close(ActionEvent event) throws IOException {
Stage stage = (Stage) cancel.getScene().getWindow();
stage.close();
}
void setTableItems(ObservableList<Part> tableItems) {
this.tableItems = tableItems;
}
}
addPart.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane id="addPage" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ims.AddPartController">
<stylesheets>
<String fx:value="style.css" />
</stylesheets>
<fx:define>
<ToggleGroup fx:id="inOutGroup" />
</fx:define>
<center>
<VBox fx:id="verticalHolding">
<children>
<HBox fx:id="topRow">
<Label text="Add Part"/>
<RadioButton fx:id="inhouse" toggleGroup="$inOutGroup" text="In-House"/>
<RadioButton fx:id="outsourced" toggleGroup="$inOutGroup" selected="true" text="Outsourced"/>
</HBox>
<HBox styleClass="fullWidth">
<HBox styleClass="halfWidthLeft">
<Label text="ID" />
</HBox>
<HBox styleClass="halfWidthRight">
<TextField promptText="Auto Gen - Disabled" disable="true" />
</HBox>
</HBox>
<HBox styleClass="fullWidth">
<HBox styleClass="halfWidthLeft">
<Label text="Name" />
</HBox>
<HBox styleClass="halfWidthRight">
<TextField fx:id="partNameField" promptText="Part Name" />
</HBox>
</HBox>
<HBox styleClass="fullWidth">
<HBox styleClass="halfWidthLeft">
<Label text="Inv" />
</HBox>
<HBox styleClass="halfWidthRight">
<TextField fx:id="partInstockField" promptText="Inv" />
</HBox>
</HBox>
<HBox styleClass="fullWidth">
<HBox styleClass="halfWidthLeft">
<Label text="Price/Cost" />
</HBox>
<HBox styleClass="halfWidthRight">
<TextField fx:id="partPriceField" promptText="Price/Cost" />
</HBox>
</HBox>
<HBox styleClass="fullWidth">
<HBox styleClass="halfWidthLeft">
<Label text="Max" />
</HBox>
<HBox styleClass="halfWidthRight">
<TextField styleClass="smallTextField" fx:id="partMaxField" promptText="Max" />
<Label text="Min" />
<TextField styleClass="smallTextField" fx:id="partMinField" promptText="Min" />
</HBox>
</HBox>
<HBox styleClass="fullWidth">
<HBox styleClass="halfWidthLeft">
<Label fx:id="inhouseLabel" text="Machine ID" />
<Label fx:id="outsourcedLabel" text="Company Name" />
</HBox>
<HBox styleClass="halfWidthRight">
<TextField fx:id="inhouseTextField" promptText="Mach ID" />
<TextField fx:id="outsourcedTextField" promptText="Comp Nm" />
</HBox>
</HBox>
<HBox styleClass="fullWidth">
<HBox styleClass="halfWidthLeft">
</HBox>
<HBox styleClass="halfWidthRight">
<Button onAction="#addInhouse" fx:id="save" text="Save" />
<Button onAction="#close" fx:id="cancel" text="Cancel" />
</HBox>
</HBox>
</children>
</VBox>
</center>
</BorderPane>

Binding listener isn't working properly in JavaFX 8

Currently I am learning how to use Bindings and Binding-Events properly. I already read a chapter of a book about it and in general I have no problem in using Bindings.
For testing my knowledge, I wrote a little JavaFX8 Application. I got 2 TextFields, but at the moment I am focusing on one TextField, called "firstName". I am using a BooleanBinding. Whenever the TextField is getting filled, the BooleanBinding is set to "true". If there's no Input in the Field, the BooleanBinding is set to "false". My goal is to update Label called "statusLabel", whenever the BooleanBinding got changed.
This is how the binding looks:
BooleanBinding nameEntered = firstName.textProperty().isNotEmpty();
This is my ChangeListener:
nameEntered.addListener((o, oldValue, newValue) -> {
statusLabel.setText(newValue.toString());
});
For a short amount of time, the Listener is working properly. When the BooleanBinding got changed, the Label is getting updated. But after some input changes (deleting the input, filling again etc...) the Label isn't getting updated anymore. Any ideas how to fix this?
Here is the full code:
FXMLController:
package gui;
/*
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.binding.StringBinding;
import javafx.beans.binding.When;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
*/
public class LayoutController implements Initializable {
/**
* Initializes the controller class.
*/
#FXML
private TextField firstName;
#FXML
private TextField secondName;
#FXML
private CheckBox checkBox1;
#FXML
private CheckBox checkBox2;
#FXML
private CheckBox checkBox3;
#FXML
private Label statusLabel;
#Override
public void initialize(URL url, ResourceBundle rb) {
BooleanBinding nameEntered = firstName.textProperty().isNotEmpty();
nameEntered.addListener((o, oldValue, newValue) -> {
statusLabel.setText(newValue.toString());
});
}
}
MainView.java:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author xyz
*/
public class MainView extends Application{
#Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Layout.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("Benutzerauswahl");
primaryStage.show();
}
public static void main(String args[]){
launch(args);
}
}
FXMLLayout:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="310.0" prefWidth="343.0" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gui.LayoutController">
<center>
<AnchorPane prefHeight="371.0" prefWidth="380.0" BorderPane.alignment="CENTER">
<children>
<GridPane layoutX="50.0" layoutY="103.0" prefHeight="234.0" prefWidth="281.0" AnchorPane.leftAnchor="50.0" AnchorPane.rightAnchor="49.0" AnchorPane.topAnchor="50.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="155.0" minWidth="10.0" prefWidth="110.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="197.0" minWidth="10.0" prefWidth="171.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="40.0" minHeight="10.0" prefHeight="40.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="40.0" minHeight="10.0" prefHeight="40.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="400.0" minHeight="10.0" prefHeight="88.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label prefHeight="17.0" prefWidth="56.0" text="Vorname:" GridPane.halignment="CENTER" />
<Label prefHeight="17.0" prefWidth="69.0" text="Nachname:" GridPane.halignment="CENTER" GridPane.rowIndex="1" />
<TextField fx:id="firstName" prefHeight="25.0" prefWidth="144.0" GridPane.columnIndex="1" />
<TextField fx:id="secondName" prefHeight="25.0" prefWidth="144.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="2">
<children>
<CheckBox fx:id="checkBox1" layoutX="14.0" layoutY="42.0" mnemonicParsing="false" text="Kurs 1" />
<CheckBox fx:id="checkBox2" layoutX="14.0" layoutY="69.0" mnemonicParsing="false" text="Kurs 2" />
<CheckBox fx:id="checkBox3" layoutX="14.0" layoutY="96.0" mnemonicParsing="false" text="Kurs 3" />
<Label fx:id="statusLabel" layoutX="43.0" layoutY="132.0" prefHeight="17.0" prefWidth="84.0" text="Status" />
</children>
</AnchorPane>
</children>
</GridPane>
</children>
</AnchorPane>
</center>
</BorderPane>
This is really a duplicate of JavaFX Beans Binding suddenly stops working: the problem is that the binding is getting "prematurely garbage collected" because there are no live references to it that are retained. Forcing the reference to be retained in a controller seems to be a little tricky.
First note that if you actually bind a property of a UI element (which is necessarily in scope as long as it is displayed), then the UI element indirectly keeps a reference to the binding. Consequently in your code this will fix the problem:
statusLabel.textProperty().bind(nameEntered.asString());
(instead of the listener you currently have). If you can't actually use a binding, then it seems that first you need to get the controller to retain a reference to the binding:
public class LayoutController implements Initializable {
// existing code...
private BooleanBinding nameEntered ;
public void initialize(URL url, ResourceBundle rb) {
nameEntered = firstName.textProperty().isNotEmpty();
nameEntered.addListener((o, oldValue, newValue) -> {
statusLabel.setText(newValue.toString());
});
}
}
and then additionally you need to force an reference to the controller itself to stay in scope:
public class MainView extends Application{
private LayoutController controller ;
#Override
public void start(Stage primaryStage) throws Exception {
FMXLLoader loader = new FXMLLoader(getClass().getResource("Layout.fxml"));
Parent root = loader.load();
controller = loader.getController();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("Benutzerauswahl");
primaryStage.show();
}
public static void main(String args[]){
launch(args);
}
}
There may be a more obvious way to do this, but I can't find one that works.

Resources