I'm trying to set up a simple example for my program using JavaFX. What I want to have is a Controller with a main.fxml; then the main.fxml will have a TabbedPane as root, with 2 tabs (tab1.fxml and tab2.fxml), each with its controller (Tab1Controller and Tab2Controller).
Now this might be the prolem, yet I don't see why it would be a problem:
Tab1Controller and Tab2Controller both extend Controller; because they share various fields which are manipulated, for example a bottom status bar which needs constant updates.
So far I managed to have working all controllers and .fxml.
When I try to set up the text of the label in the Controller from one of the subclasses it just points to null, yet the label is initialised in the gui with its default text.. Compiles correctly and lblController seems to be correctly linked to its fx:id in main.fxml.
Any help/links are much appreciated.
I've been looking at various posts but the only one getting close to what I need it this one: JavaFX 8, controllers inheritance
Main:
package sample;
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 {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("../view/main.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 600, 700));
primaryStage.show();
}
}
Main controller:
package sample;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class Controller {
@FXML
public Label lblController;
public Controller() {
System.out.println("CONTROLLER MAIN - CONSTRUCTOR");
}
@FXML
public void initialize() {
System.out.println("CONTROLLER MAIN - INITIALIZER");
}
protected void setSts(String sts) {
lblController.setText(sts);
}
}
main.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.*?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1"
xmlns="http://javafx.com/javafx/8.0.60"
fx:controller="sample.Controller">
<BorderPane>
<center>
<TabPane tabClosingPolicy="UNAVAILABLE">
<Tab text="Tab 1">
<AnchorPane>
<!--FOR fx:id HAS TO HAVE THE SAME NAME BUT IN LOWER CASE AS THE .fxml-->
<fx:include fx:id="tab1" source="tab/tab1.fxml"/>
</AnchorPane>
</Tab>
<Tab text="Tab 2">
<AnchorPane>
<fx:include fx:id="tab2" source="tab/tab2.fxml"/>
</AnchorPane>
</Tab>
</TabPane>
</center>
<bottom>
<AnchorPane>
<Label fx:id="lblController" text="Controller Test"/>
</AnchorPane>
</bottom>
</BorderPane>
</AnchorPane>
Tab1Controller
package sample.ctrTab;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import sample.Controller;
public class Tab1Controller extends Controller {
@FXML
Label lbl1;
@FXML
TextField txt1;
@FXML
Button btn1Save, btn1Load;
public Tab1Controller() {
super();
System.out.println("CONTROLLER TAB 1 - CONSTRUCTOR");
}
@FXML
void btn1Save() {
System.out.println("CONTROLLER TAB 1 - SAVE CLICK");
// lblController.setText("ANYTHING"); //NULL POINTER HERE
// setSts(txt1.getText()); //OR HERE
}
@FXML
void btn1Load() {
System.out.println("CONTROLLER TAB 1 - LOAD CLICK");
}
protected void setSts(String sts) {
super.setSts(sts);
}
}
tab1.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1"
xmlns="http://javafx.com/javafx/8.0.60" fx:controller="sample.ctrTab.Tab1Controller">
<children>
<Label fx:id="lbl1" layoutX="100" layoutY="50" text="Def tab 1"/>
<TextField fx:id="txt1" layoutX="100" layoutY="70"/>
<Button fx:id="btn1Save" onAction="#btn1Save" layoutX="100" layoutY="140" mnemonicParsing="false"
text="Save text"/>
<Button fx:id="btn1Load" onAction="#btn1Load" layoutX="200.0" layoutY="140" mnemonicParsing="false"
text="Send to tab 2"/>
</children>
</AnchorPane>