I have been trying to figure out how to deal with a FXML file in another FXML file. But I have an exception. :( This is my inner FXML (NewInside.fxml in view package):
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="305.0" prefWidth="360.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="controller.NewInsideController">
<children>
<Button fx:id="newBtn" layoutX="30.0" layoutY="202.0" mnemonicParsing="false" onAction="#btnClick" text="Button" />
<TextField fx:id="newTxt" layoutX="30.0" layoutY="134.0" prefHeight="25.0" prefWidth="280.0" />
<Label fx:id="newLbl" layoutX="30.0" layoutY="62.0" prefHeight="17.0" prefWidth="280.0" />
</children>
</AnchorPane>
This is its controller (NewInsideController.java in controller package):
package controller;
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.control.TextField;
public class NewInsideController implements Initializable{
@FXML public static Label newLbl;
@FXML public static TextField newTxt;
@FXML public static Button newBtn;
@Override
public void initialize(URL location, ResourceBundle resources) {
}
@FXML
private void btnClick(ActionEvent e){
System.out.println("Button is clicked!");
newLbl.setText(newTxt.getText());
}
}
And this is outer FXML (New.fxml in view package):
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="380.0" prefWidth="529.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="controller.NewController">
<children>
<fx:include source="NewInside.fxml" />
</children>
</AnchorPane>
This is its controller (NewController.java in controller package):
package controller;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
public class NewController implements Initializable {
/*@FXML private Label newLbl;
@FXML private Button newBtn;
@FXML private TextField newTxt;
*/
@Override
public void initialize(URL location, ResourceBundle resources) {
}
/*@FXML
public void btnClick(ActionEvent e){
newLbl.setText(newTxt.getText());
}*/
}
Finally, this is the Main.java in application package:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/view/New.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
When I clicked the button, it gives me very long exception: "Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException.... ..... Caused by: java.lang.NullPointerException at controller.NewInsideController.btnClick(NewInsideController.java:27) ... 57 more"
Please help me! :(