i am trying to navigate to an other fxml file but when i try parent root = loader.load() he catches an exception and throws it back. He prints SYSTEEMFOUT bij laden Fietsenbeheer: but no message with it. laadLedenBeheer is the one that works fine and laadFietsenBeheer doens't.
Here is my main program:
public void laadLedenbeheer() {
try {
String fxmlFile = "/fxml/LedenBeheer.fxml";
FXMLLoader loader = new FXMLLoader(getClass().getResource(fxmlFile));
// controller instellen
LedenBeheerController controller = new LedenBeheerController(this,createLidservice());
loader.setController(controller);
Parent root = loader.load();
controller.initialiseertabel();
controller.setParent(this);
Scene scene = new Scene(root);
stage.setTitle("Leden beheren");
stage.setScene(scene);
} catch (IOException e) {
System.out.println("SYSTEEMFOUT bij laden ledenbeheer: " + e.getMessage());
}
}
public void laadFietsbeheer() {
try {
String fxmlFile = "/fxml/FietsenBeheer.fxml";
FXMLLoader loader = new FXMLLoader(getClass().getResource(fxmlFile));
// controller instellen
FietsenBeheerController controller = new FietsenBeheerController(this, createFietsService());
loader.setController(controller);
Parent root = loader.load();
controller.setParent(this);
Scene scene = new Scene(root);
stage.setTitle("Fietsen beheren");
stage.setScene(scene);
} catch (IOException e) {
System.out.println("SYSTEEMFOUT bij laden Fietsenbeheer: " + e.getMessage());
}
}
Here is my fxml file that i am trying to load
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.cell.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="676.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<center>
<GridPane BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<BorderPane prefHeight="200.0" prefWidth="200.0">
<center>
<TableView fx:id="tvLeden" prefHeight="506.0" prefWidth="431.0" BorderPane.alignment="CENTER">
<columns>
<TableColumn fx:id="tcVoornaam" prefWidth="99.0" text="Voornaam">
<cellValueFactory>
<PropertyValueFactory property="voornaam" />
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="tcNaam" prefWidth="97.0" text="Naam">
<cellValueFactory>
<PropertyValueFactory property="naam" />
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="tcRijksreg" minWidth="0.0" prefWidth="184.0" text="Rijksregisternummer">
<cellValueFactory>
<PropertyValueFactory property="rijksregisternummer" />
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
</center>
<bottom>
<FlowPane alignment="CENTER" columnHalignment="CENTER" hgap="20.0" prefHeight="50.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<Button fx:id="btnFietsbeheer" mnemonicParsing="false" onAction="#fietsen" text="Fietsbeheer" />
<Button fx:id="btnWijzigenLid" mnemonicParsing="false" onAction="#wijzigenLid" text="Wijzigen" />
<Button fx:id="btnStartdatum" mnemonicParsing="false" onAction="#wijzigenStartdatum" text="Startdatum wijzigen" />
<Button fx:id="btnUitschrijvenLid" mnemonicParsing="false" onAction="#uitschrijvenLid" text="Uitschrijven" />
</children>
</FlowPane>
</bottom>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</BorderPane>
<BorderPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1">
<center>
<FlowPane orientation="VERTICAL" prefHeight="200.0" prefWidth="200.0" vgap="20.0">
<children>
<GridPane hgap="20.0" vgap="5.0">
<children>
<TextField fx:id="tfVoornaam" promptText="Voornaam" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<TextField fx:id="tfNaam" promptText="Naam" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<TextField fx:id="tfRijksregisternummer" prefHeight="25.0" prefWidth="134.0" promptText="Rijksregisternummer" GridPane.columnIndex="1" />
<TextField fx:id="tfEmail" promptText="Email" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<DatePicker fx:id="dpStartdatum" disable="true" editable="false" promptText="Startdatum" GridPane.columnIndex="1" GridPane.rowIndex="5" />
<CheckBox fx:id="cbUitgeschreven" disable="true" mnemonicParsing="false" text="Uitgeschreven" GridPane.columnIndex="1" GridPane.rowIndex="6" GridPane.valignment="TOP" />
<TextArea fx:id="taOpmerking" prefHeight="200.0" prefWidth="200.0" GridPane.columnSpan="2" GridPane.rowIndex="8" GridPane.rowSpan="2" />
<Label text="Rijksregisternummer" />
<Label text="Voornaam" GridPane.rowIndex="1" />
<Label text="Naam" GridPane.rowIndex="2" />
<Label text="E-mail" GridPane.rowIndex="3" />
<Label text="Startdatum" GridPane.rowIndex="5" />
<Label text="Opmerking" GridPane.rowIndex="7" />
</children>
<columnConstraints>
<ColumnConstraints maxWidth="131.0" minWidth="122.0" prefWidth="122.0" />
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" />
<RowConstraints minHeight="10.0" prefHeight="30.0" />
<RowConstraints minHeight="10.0" prefHeight="30.0" />
<RowConstraints minHeight="10.0" prefHeight="30.0" />
<RowConstraints maxHeight="0.0" minHeight="0.0" prefHeight="0.0" />
<RowConstraints maxHeight="25.0" minHeight="25.0" prefHeight="25.0" />
<RowConstraints />
<RowConstraints />
<RowConstraints />
<RowConstraints />
</rowConstraints>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</GridPane>
</children>
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
</FlowPane>
</center>
<bottom>
<FlowPane alignment="CENTER" columnHalignment="CENTER" hgap="20.0" prefHeight="50.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<Button fx:id="btnAnnuleren" mnemonicParsing="false" onAction="#annuleren" text="Annuleren" />
<Button fx:id="btnOpslaan" mnemonicParsing="false" onAction="#opslaan" text="Ok" />
</children>
</FlowPane>
</bottom>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</BorderPane>
</children>
</GridPane>
</center>
<top>
<FlowPane alignment="CENTER" columnHalignment="CENTER" orientation="VERTICAL" prefHeight="100.0" BorderPane.alignment="CENTER">
<children>
<Label text="Ledenbeheer">
<font>
<Font size="42.0" />
</font>
</Label>
<Label fx:id="laErrorLeden" textFill="RED" />
</children>
</FlowPane>
</top>
</BorderPane>
anyone an idea how to solve this?
Related
I'm newbie in Java FXML. I want to add an item to the TableView in first stage(tableview.fxml) from form I've created in second stage(dodawanie.fxml). Can someone tell me how to do this?
Calling the method AddPersonToTable from DodawanieController causes that ObservableList data is updated, but the TableView not.
TableViewController.java
public class TableViewController {
#FXML public TableView<Person> tableView;
#FXML private TextField firstNameField;
#FXML private TextField lastNameField;
#FXML private TextField emailField;
#FXML
protected void addPerson(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("Dodawanie.fxml"));
Stage stage = new Stage();
stage.setTitle("Dodaj ksiazke");
stage.setScene(new Scene(root));
stage.show();
}
public void updateTable(Person person){
ObservableList<Person> data = tableView.getItems();
data.add(person);
tableView.setItems(data);
}
}
DodawanieController.java
public class DodawanieController {
#FXML
private TextField firstNameField;
#FXML
private TextField lastNameField;
#FXML
private TextField emailField;
#FXML
public void AddPersonToTable(ActionEvent actionEvent) throws IOException {
Person person = new Person(firstNameField.getText(), lastNameField.getText(), emailField.getText());
System.out.println(person);
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("tableview.fxml"));
loader.load();
TableViewController tableViewController = loader.getController();
tableViewController.updateTable(person);
}
}
tableview.fxml
<GridPane alignment="CENTER" hgap="10.0" vgap="10.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.TableViewController">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
<Label style="-fx-font: NORMAL 20 Tahoma;" text="Address Book" GridPane.columnIndex="0" GridPane.rowIndex="0">
</Label>
<TableView fx:id="tableView" GridPane.columnIndex="0" GridPane.rowIndex="1">
<columns>
<TableColumn text="First Name">
<cellValueFactory><PropertyValueFactory property="firstName" />
</cellValueFactory>
</TableColumn>
<TableColumn text="Last Name">
<cellValueFactory><PropertyValueFactory property="lastName" />
</cellValueFactory>
</TableColumn>
<TableColumn text="Email Address">
<cellValueFactory><PropertyValueFactory property="email" />
</cellValueFactory>
</TableColumn>
</columns>
<items>
<FXCollections fx:factory="observableArrayList">
<Person email="jacob.smith#example.com" firstName="Jacob" lastName="Smith" />
<Person email="isabella.johnson#example.com" firstName="Isabella" lastName="Johnson" />
<Person email="ethan.williams#example.com" firstName="Ethan" lastName="Williams" />
<Person email="emma.jones#example.com" firstName="Emma" lastName="Jones" />
<Person email="michael.brown#example.com" firstName="Michael" lastName="Brown" />
</FXCollections>
</items>
</TableView>
<Button alignment="CENTER_RIGHT" onAction="#addPerson" text="Add" GridPane.rowIndex="2" />
<HBox prefHeight="100.0" prefWidth="200.0" GridPane.rowIndex="3">
<children>
<TextField fx:id="firstNameField" />
<TextField fx:id="lastNameField" />
<TextField fx:id="emailField" />
</children>
</HBox>
<Button onAction="#addPerson2" mnemonicParsing="false" text="Dodaj" GridPane.rowIndex="4" />
<columnConstraints>
<ColumnConstraints />
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
<RowConstraints />
<RowConstraints />
<RowConstraints />
<RowConstraints />
<RowConstraints />
</rowConstraints>
</GridPane>
Dodawanie.fxml
<GridPane alignment="CENTER" hgap="10.0" prefHeight="400" prefWidth="600" vgap="10.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.DodawanieController">
<columnConstraints>
<ColumnConstraints />
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
</rowConstraints>
<children>
<VBox prefHeight="200.0" prefWidth="100.0" spacing="10">
<children>
<Label text="First Name" />
<Label text="Last Name" />
<Label text="Email" />
</children>
</VBox>
<VBox prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="1" spacing="10">
<children>
<TextField fx:id="firstNameField" />
<TextField fx:id="lastNameField" />
<TextField fx:id="emailField" />
<Button mnemonicParsing="false" text="Button" onAction="#AddPersonToTable"/>
</children>
</VBox>
</children>
</GridPane>
I want to add a listener to a textField in a window that appears when I press a button on a previous window. The problem is that the listener doesn't work at all. It doesn't detect any change.
public class WindowTwoController {
private Stage stage;
#FXML
private TextField imieTF = new TextField();
public void show() throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("winTwo.fxml"));
Parent root = loader.load();
stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setTitle("Sell Art");
stage.setScene(new Scene(root, 500, 500));
imieTF.textProperty().addListener((observable, oldValue, newValue) -> {
System.out.println("textfield changed from " + oldValue + " to " + newValue);
});
stage.showAndWait();
}
I'm changing the value of the textField, but nothing is printed out to the console. The show method is called when a button is pressed on the previous window. Please help me. This is my winTwo.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<GridPane alignment="center" hgap="10" prefHeight="441.0" prefWidth="500.0" vgap="10" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="windowTwo.WindowTwoController">
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
</rowConstraints>
<children>
<VBox prefHeight="354.0" prefWidth="455.0">
<children>
<Label alignment="CENTER" prefHeight="45.0" prefWidth="457.0" text="Sprzedaż dzieła">
<font>
<Font size="30.0" />
</font>
</Label>
<Separator prefWidth="200.0" />
<GridPane prefHeight="139.0" prefWidth="455.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label alignment="CENTER" prefHeight="40.0" prefWidth="220.0" text="Imię">
<font>
<Font size="28.0" />
</font>
</Label>
<Label alignment="CENTER" prefHeight="40.0" prefWidth="220.0" text="Nazwisko" GridPane.rowIndex="1">
<font>
<Font size="28.0" />
</font>
</Label>
<TextField fx:id="imieTF" GridPane.columnIndex="1">
<GridPane.margin>
<Insets right="20.0" />
</GridPane.margin></TextField>
<TextField fx:id="nazwiskoTF" GridPane.columnIndex="1" GridPane.rowIndex="1">
<GridPane.margin>
<Insets right="20.0" />
</GridPane.margin>
</TextField>
</children>
</GridPane>
<Separator prefWidth="200.0" />
<Label alignment="CENTER" prefHeight="17.0" prefWidth="450.0" text="Klient powracający">
<font>
<Font size="22.0" />
</font>
</Label>
<Separator prefWidth="200.0" />
<GridPane prefHeight="126.0" prefWidth="455.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label alignment="CENTER" prefHeight="40.0" prefWidth="220.0" text="Poziom Zaprzyjaźnienia" textFill="#00000080">
<font>
<Font size="19.0" />
</font>
</Label>
<ComboBox fx:id="cb" opacity="0.5" prefHeight="25.0" prefWidth="207.0" GridPane.columnIndex="1" />
<Button mnemonicParsing="false" prefHeight="25.0" prefWidth="175.0" text="Akceptuj" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="25.0" />
</GridPane.margin>
</Button>
<Button mnemonicParsing="false" prefHeight="25.0" prefWidth="175.0" text="Anuluj" GridPane.columnIndex="1" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="27.0" />
</GridPane.margin>
</Button>
</children>
</GridPane>
</children>
</VBox>
</children>
</GridPane>
You should add an anotation to the private member imieTF like so
public class WindowTwoController {
#FXML
private TextField imieTF;
#FXML
private void initialize() {
imieTF.textProperty().addListener((observable, oldValue, newValue) ->
{
System.out.println("textfield changed from " + oldValue + " to " + newValue);
});
}
public static void show() {
FXMLLoader loader = new FXMLLoader(getClass().getResource("winTwo.fxml"));
Parent root = loader.load();
stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setTitle("Sell Art");
stage.setScene(new Scene(root, 500, 500));
stage.showAndWait();
}
}
This should bind the TextField instance to the controller. But from what I can find out the FXML will create a new instance of WindowTwoController when the window belonging to the fxml file gets created.
Also see https://www.callicoder.com/javafx-fxml-form-gui-tutorial/ for a basic example on how this works.
Note that all manipulations of the textfield should be part of the JavaFX flow and cannot be done in a manual instance of WindowTwoController. Keep in mind that JavaFX will create it's own instance of WindowTwoController during the loader.load(); operation.
i have application layout:
https://i.stack.imgur.com/YsK5R.png
as a result of writing code I get this
https://i.stack.imgur.com/j2Nym.png
Question: How to remove the top line with closing tabs and leave only as on the layout?
project structure:
-[src]
.....|-[sample]
..............|- Main
..............|- sample.fxml
..............|- SampleController
..............|- Tab1.fxml
..............|- Tab1Controller
..............|- Tab2.fxml
..............|- Tab2Controller
CODE
class Main
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
private Stage primary;
private BorderPane rootLayout;
#Override
public void start(Stage primary) throws Exception{
this.primary = primary;
initRootLayout();
showTab();
}
private void initRootLayout() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("sample.fxml"));
rootLayout = loader.load();
Scene scene = new Scene(rootLayout);
primary.setScene(scene);
primary.show();
}catch (IOException e){
e.printStackTrace();}
}
private void showTab() {
try {
FXMLLoader loaderTab1 = new FXMLLoader();
loaderTab1.setLocation(Main.class.getResource("Tab1.fxml"));
FXMLLoader loaderTab2 = new FXMLLoader();
loaderTab2.setLocation(Main.class.getResource("Tab2.fxml"));
TabPane tabPane = new TabPane();
Tab tab1 = new Tab();
Tab tab2 = new Tab();
tab1.setContent(loaderTab1.load());
tab2.setContent(loaderTab2.load());
tabPane.getTabs().addAll(tab1,tab2);
rootLayout.setCenter(tabPane);
} catch (IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.SampleController">
<top>
<MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
</BorderPane>
Tab1.fxml = Tab2.fxml = ... = Tab{n}.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<TabPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Tab1Controller">
<tabs>
<Tab text="Tab1">
<content>
<SplitPane dividerPositions="0.29797979797979796" prefHeight="160.0" prefWidth="200.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<TableView fixedCellSize="1.0" layoutX="-12.0" layoutY="27.0" prefHeight="371.0" prefWidth="176.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn prefWidth="75.0" text="C1" />
<TableColumn prefWidth="75.0" text="C2" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
</children></AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<Label layoutY="14.0" text="Label" AnchorPane.leftAnchor="5.0" AnchorPane.topAnchor="14.0" />
<GridPane layoutX="98.0" layoutY="125.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="35.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Label" />
<Label text="Label" GridPane.rowIndex="1" />
<Label text="Label" GridPane.rowIndex="2" />
<Label text="Label" GridPane.columnIndex="1" />
<Label text="Label" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label text="Label" GridPane.columnIndex="1" GridPane.rowIndex="2" />
</children>
</GridPane>
</children></AnchorPane>
</items>
</SplitPane>
</content></Tab>
</tabs>
</TabPane>
this question is double from this: https://ru.stackoverflow.com/questions/941060/%D0%A3%D0%B1%D1%80%D0%B0%D1%82%D1%8C-%D0%B2%D1%82%D0%BE%D1%80%D0%B8%D1%87%D0%BD%D1%8B%D0%B5-%D0%B2%D0%BA%D0%BB%D0%B0%D0%B4%D0%BA%D0%B8
There are many way to do this depending on the genericity you want to add to your application/layout.
But the most important thing is that you seem to make a mistake by mixing up the TabPane and the Tabs. While the first one is the container, the second(s) one(s) correspond to each tab you could have. Usually you have one TabPane which contain many Tab(s) (even if it is not necessary).
One solution could be to modify your method showTab by doing this (read comment)
private void showTab() {
try {
// Tab 1 loader
FXMLLoader loaderTab1 = new FXMLLoader(Main.class.getResource("Tab1.fxml"));
// Tab 2 loader
FXMLLoader loaderTab2 = new FXMLLoader(Main.class.getResource("Tab2.fxml"));
TabPane tabPane = new TabPane();
// Remove the 4 following lines, Tabs are already declared in FXML
//Tab tab1 = new Tab();
//Tab tab2 = new Tab();
//tab1.setContent(loaderTab1.load());
//tab2.setContent(loaderTab2.load());
Tab tab1 = loaderTab1.load();
Tab tab2 = loaderTab2.load();
tabPane.getTabs().addAll(tab1,tab2);
rootLayout.setCenter(tabPane);
} catch (IOException e){
e.printStackTrace();
}
}
And change your FXML files like the following one (TabPane and tabs tags are removed) :
<Tab xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Tab1Controller" text="Tab1">
<content>
<SplitPane dividerPositions="0.29797979797979796" prefHeight="160.0" prefWidth="200.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<TableView fixedCellSize="1.0" layoutX="-12.0" layoutY="27.0" prefHeight="371.0" prefWidth="176.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn prefWidth="75.0" text="C1" />
<TableColumn prefWidth="75.0" text="C2" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
</children></AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<Label layoutY="14.0" text="Label" AnchorPane.leftAnchor="5.0" AnchorPane.topAnchor="14.0" />
<GridPane layoutX="98.0" layoutY="125.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="35.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Label" />
<Label text="Label" GridPane.rowIndex="1" />
<Label text="Label" GridPane.rowIndex="2" />
<Label text="Label" GridPane.columnIndex="1" />
<Label text="Label" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label text="Label" GridPane.columnIndex="1" GridPane.rowIndex="2" />
</children>
</GridPane>
</children></AnchorPane>
</items>
</SplitPane>
</content>
</Tab>
I'm having this error while following a tutorial:
javafx.fxml.LoadException: BorderPane is not a valid type.
/C:/Users/Eduardo%20Abreu/Documents/Eclipse-Workspace/UnifacsProjeto/bin/projeto/resources/RootLayout.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.createElement(FXMLLoader.java:2774)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2704)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at projeto.MainApp.initRootLayout(MainApp.java:33)
at projeto.MainApp.start(MainApp.java:24)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
at projeto.MainApp.showFilmeOverview(MainApp.java:48)
at projeto.MainApp.start(MainApp.java:26)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
... 1 more
Exception running application projeto.MainApp
Here's my code:
package projeto;
import java.io.IOException;
import ch.makery.address.MainApp;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class MainApp extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
#Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("CineTudo");
initRootLayout();
showFilmeOverview();
}
public void initRootLayout(){
try {
//Carrega o layout root do arquivo fxml
FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("resources/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
Scene cena = new Scene(rootLayout);
primaryStage.setScene(cena);
primaryStage.show();
} catch(IOException e) {
e.printStackTrace();
}
}
public void showFilmeOverview() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation((MainApp.class.getResource("resources/FilmeOverview.fxml")));
AnchorPane filmeOverview = (AnchorPane) loader.load();
rootLayout.setCenter(filmeOverview);
}catch (IOException e){
e.printStackTrace();
}
}
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
Here's the tutorial code:
package ch.makery.address;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class MainApp extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
#Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("AddressApp");
initRootLayout();
showPersonOverview();
}
/**
* Initializes the root layout.
*/
public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Shows the person overview inside the root layout.
*/
public void showPersonOverview() {
try {
// Load person overview.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/PersonOverview.fxml"));
AnchorPane personOverview = (AnchorPane) loader.load();
// Set person overview into the center of root layout.
rootLayout.setCenter(personOverview);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Returns the main stage.
* #return
*/
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
I don't have any id's on the Fxml file and the tutorial's one doesn't have it either, i created FilmeOverview.fxml as AnchorPane and RootLayout as BorderPane, I tried changing the position of the method but it doesn't work, does anyone know how to fix this?
Edit: Here's the FXML file:
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="500.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<children>
<SplitPane dividerPositions="0.29797979797979796" layoutX="191.0" layoutY="120.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<TableView layoutX="-25.0" layoutY="46.0" prefHeight="398.0" prefWidth="175.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn prefWidth="75.0" text="Filme" />
<TableColumn prefWidth="75.0" text="Sala" />
<TableColumn prefWidth="75.0" text="Categoria" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<Label layoutX="14.0" layoutY="14.0" text="Detalhes do Filme">
<font>
<Font name="Open Sans Light" size="15.0" />
</font>
</Label>
<GridPane layoutX="14.0" layoutY="49.0" prefHeight="268.0" prefWidth="547.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="49.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Título:">
<font>
<Font size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
<Label layoutX="20.0" layoutY="17.0" text="Sala:" GridPane.rowIndex="1">
<font>
<Font size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
<Label layoutX="20.0" layoutY="55.0" text="Categoria:" GridPane.rowIndex="2">
<font>
<Font size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
<Label text="Diretor:" GridPane.rowIndex="3">
<font>
<Font size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
<Label text="Duração:" GridPane.rowIndex="4">
<font>
<Font size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
<Label text="Protagonista:" GridPane.rowIndex="5">
<font>
<Font size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
<Label text="Classificação:" GridPane.rowIndex="6">
<font>
<Font size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
<Label text="Label" GridPane.columnIndex="1">
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
<font>
<Font name="Roboto" size="16.0" />
</font>
</Label>
<Label text="Label" GridPane.columnIndex="1" GridPane.rowIndex="1">
<font>
<Font name="Roboto" size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
<Label text="Label" GridPane.columnIndex="1" GridPane.rowIndex="2">
<font>
<Font name="Roboto" size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
<Label text="Label" GridPane.columnIndex="1" GridPane.rowIndex="3">
<font>
<Font name="Roboto" size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
<Label text="Label" GridPane.columnIndex="1" GridPane.rowIndex="4">
<font>
<Font name="Roboto" size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
<Label text="Label" GridPane.columnIndex="1" GridPane.rowIndex="5">
<font>
<Font name="Roboto" size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
<Label text="Label" GridPane.columnIndex="1" GridPane.rowIndex="6">
<font>
<Font name="Roboto" size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
</children>
</GridPane>
<ButtonBar layoutX="276.0" layoutY="425.0" prefHeight="59.0" prefWidth="267.0">
<buttons>
<JFXButton style="-fx-background-color: #00BCD4;" text="Adicionar" />
<JFXButton style="-fx-background-color: #03A9F4;" text="Editar" />
<JFXButton style="-fx-background-color: #F44336;" text="Deletar" />
</buttons>
</ButtonBar>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
Edit 2: Here's rootLayout.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.AnchorPane?>
<BorderPane prefHeight="500.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<children>
<MenuBar layoutY="2.0" prefHeight="25.0" prefWidth="800.0">
<menus>
<Menu mnemonicParsing="false" text="Arquivo">
<items>
<MenuItem mnemonicParsing="false" text="Fechar" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Editar">
<items>
<MenuItem mnemonicParsing="false" text="Deletar" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Ajuda">
<items>
<MenuItem mnemonicParsing="false" text="Sobre" />
</items>
</Menu>
</menus>
</MenuBar>
</children>
</AnchorPane>
This is not a duplicate, changing the import doesn't fix it, i still get
Caused by: javax.xml.stream.XMLStreamException: ParseError at
[row,col]:[31,3]
You have a couple of issues:
The FXML doesn't have an import statement for javafx.scene.layout.BorderPane.
Your rootLayout.fxml file is invalid FXML. You close the BorderPane element with an AnchorPane tag.
You define the contents of the border pane as children, where they are supposed to be defined by position.
Sample valid FXML with the above issues fixed:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane prefHeight="500.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<top>
<MenuBar layoutY="2.0" prefHeight="25.0" prefWidth="800.0">
<menus>
<Menu mnemonicParsing="false" text="Arquivo">
<items>
<MenuItem mnemonicParsing="false" text="Fechar" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Editar">
<items>
<MenuItem mnemonicParsing="false" text="Deletar" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Ajuda">
<items>
<MenuItem mnemonicParsing="false" text="Sobre" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
</BorderPane>
As an aside, I recommend downloading Gluon SceneBuilder and using that to edit your FXML.
Also note, some IDEs, such as IntelliJ, include code completion, type checking and syntax checking which would highlight many of the above errors in your document when you load the file into the IDE.
I am trying to set the text to different labels using setText() method. I do tried to print the value to check whether the setText receives null or not. But I am able to successfully print the value in the console but why not able to set it to a label is what my question is.
Any suggestions would be helpful to resolve this NullPointerException.
PersonOverviewController.java
package passion.controllers;
import passion.models.Person;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
public class PersonOverviewController{
#FXML
private TableView<Person> personTable;
#FXML
private TableColumn<Person, String> firstNameColumn;
#FXML
private TableColumn<Person, String> lastNameColumn;
#FXML
private Label firstNameLabel;
#FXML
private Label lastNameLabel;
#FXML
private Label streetLabel;
#FXML
private Label postalCodeLabel;
#FXML
private Label cityLabel;
#FXML
private Label birthdayLabel;
public PersonOverviewController() {
}
#FXML
private void initialize() {
// Initialize the person table with the two columns.
firstNameColumn.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
lastNameColumn.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty());
try{
//showPersonDetails(null);
//personTable.getSelectionModel().selectedItemProperty().addListener( (observable, oldValue, newValue) -> showPersonDetails(newValue));
personTable.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Person>(){
#Override
public void changed(ObservableValue<? extends Person> observable, Person oldValue, Person newValue) {
System.out.println(newValue);
showPersonDetails(newValue);
}
});
}catch(NullPointerException e){
e.printStackTrace();
}
}
public void showTableViewData(MainApp mainApp) {
personTable.setItems(mainApp.getPersonData());
}
public void showPersonDetails(Person person){
Platform.runLater(new Runnable(){
#Override
public void run() {
if(person != null){
System.out.println(person.getFirstName());
firstNameLabel.setText(person.getFirstName());
System.out.println("test");
lastNameLabel.setText(person.getLastName());
streetLabel.setText(person.getStreet());
postalCodeLabel.setText(Integer.toString(person.getPostalCode()));
cityLabel.setText(person.getCity());
birthdayLabel.setText(person.getBirthday().toString());
}else{
firstNameLabel.setText("");
lastNameLabel.setText("");
streetLabel.setText("");
postalCodeLabel.setText("");
cityLabel.setText("");
birthdayLabel.setText("");
}
}
});
}
}
This is how I am loading the controller from MainApp.java
public void showPersonOverview(){
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("/passion/viewControllers/PersonOverView.fxml"));
AnchorPane personOverview = (AnchorPane)loader.load();
root.setCenter(personOverview);
PersonOverviewController personController = loader.getController();
personController.showTableViewData(this);
} catch (IOException e) {
e.printStackTrace();
}
}
PersonOverview.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="300.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="passion.controllers.PersonOverviewController">
<children>
<SplitPane dividerPositions="0.29797979797979796" layoutX="200.0" layoutY="70.0" prefHeight="300.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<TableView fx:id="personTable" layoutX="-12.0" layoutY="49.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="-0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn fx:id="firstNameColumn" prefWidth="75.0" text="First Name" />
<TableColumn fx:id="lastNameColumn" prefWidth="75.0" text="Last Name" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<Label layoutX="14.0" layoutY="14.0" text="Person Details:" AnchorPane.leftAnchor="5.0" AnchorPane.topAnchor="5.0" />
<GridPane layoutX="34.0" layoutY="38.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="30.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="First Name">
<GridPane.margin>
<Insets left="5.0" />
</GridPane.margin>
</Label>
<Label text="Last Name" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="5.0" />
</GridPane.margin>
</Label>
<Label text="Street" GridPane.rowIndex="2">
<GridPane.margin>
<Insets left="5.0" />
</GridPane.margin>
</Label>
<Label text="City" GridPane.rowIndex="3">
<padding>
<Insets left="5.0" />
</padding>
</Label>
<Label text="Postal Code" GridPane.rowIndex="4">
<GridPane.margin>
<Insets left="5.0" />
</GridPane.margin>
</Label>
<Label text="Birthday" GridPane.rowIndex="5">
<GridPane.margin>
<Insets left="5.0" />
</GridPane.margin>
</Label>
<Label fx:id="firstName" text="Label" GridPane.columnIndex="1">
<padding>
<Insets left="5.0" />
</padding>
</Label>
<Label fx:id="lastName" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="5.0" />
</GridPane.margin>
</Label>
<Label fx:id="street" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="2">
<GridPane.margin>
<Insets left="5.0" />
</GridPane.margin>
</Label>
<Label fx:id="city" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="3">
<GridPane.margin>
<Insets left="5.0" />
</GridPane.margin>
</Label>
<Label fx:id="postal" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="4">
<GridPane.margin>
<Insets left="5.0" />
</GridPane.margin>
</Label>
<Label fx:id="birthday" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="5">
<GridPane.margin>
<Insets left="5.0" />
</GridPane.margin>
</Label>
</children>
</GridPane>
<HBox layoutX="229.0" layoutY="244.0" spacing="5.0" AnchorPane.bottomAnchor="10.0" AnchorPane.rightAnchor="10.0">
<children>
<Button layoutX="229.0" layoutY="244.0" mnemonicParsing="false" text="New..." />
<Button layoutX="289.0" layoutY="244.0" mnemonicParsing="false" text="Edit..." />
<Button layoutX="351.0" layoutY="244.0" mnemonicParsing="false" text="Delete" />
</children>
</HBox>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
Console:
Person [firstName=StringProperty [value: Satya], lastName=StringProperty [value: Vema], street=StringProperty [value: some street], postalCode=IntegerProperty [value: 1234], city=StringProperty [value: some city], birthday=ObjectProperty [value: 1999-02-21]]
Person [firstName=StringProperty [value: Satya], lastName=StringProperty [value: Vema], street=StringProperty [value: some street], postalCode=IntegerProperty [value: 1234], city=StringProperty [value: some city], birthday=ObjectProperty [value: 1999-02-21]]
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at passion.controllers.PersonOverviewController$2.run(PersonOverviewController.java:69)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/32693051.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/2180324.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/3326003.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
The connection (fx:id to variable name #FXML or #FXML("...")) is missing.
<Label text="First Name" fx:id="firstNameLabel">
Hence the components cannot be set to the FXML generated dialog's actual components.
I think you are missing one part of code.
TextView firstNameLabel = (TextView)findIdById(R.id.yourTextViewIdInLayout);
And you must do that with all your widgets in order not to get a NullPointerException.