LoWesT LoWesT -4 years ago 187
Java Question

JavaFX8 - Tabs with separate FXML and controllers

I'm trying to include FXML files in the tabs of my mainview.fxml and for those I would like to use different controllers. I did this a year ago and it worked, but when I tried it again my other controllers are all null, except the main one.

This is my code for the mainframe controller:

public class MainFrameController {

private static final Logger LOGGER = LoggerFactory.getLogger(MainFrameController.class);
protected ServiceInterface service;
protected Stage primaryStage;
protected Stage stage;
protected MODE mode;

@FXML
private BoxFrameController boxFrameController;

@FXML
private ReservationFrameController reservationFrameController;

@FXML
private TabPane tabPane;

@FXML
private Tab boxTab;

private void loadTestData() {
LOGGER.info("GUI: Loading test data");
try {
service.loadTestData();
boxFrameController.setService(service);
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
}

public void setPrimaryStage(Stage primaryStage) {
this.primaryStage = primaryStage;
//boxFrameController.setPrimaryStage(primaryStage);
}

public void setStage(Stage stage) {
this.stage = stage;
}

public void setMODE(MODE mode) {
this.mode = mode;
}

public void setService (ServiceInterface service) throws ServiceException {
this.service = service;
//setup controllers to switch tabs
boxFrameController.setService(service);
//boxFrameController.fillTable(service);
//reservationFrameController.setService(this.service);
}
}


Here is my MainFrame.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.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>

<AnchorPane prefHeight="800.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sepm.ss17.e1326220.gui.MainFrameController">
<children>
<VBox layoutX="-1.0" layoutY="-1.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<MenuBar>
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
<TabPane fx:id="tabPane" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab fx:id="boxTab" closable="false" text="Boxes">
<content>
<fx:include fx:id="box" source="BoxFrame.fxml" />
</content>
</Tab>
<Tab closable="false" text="Reservations" />
<Tab closable="false" text="Invoices" />
<Tab closable="false" text="Statistic" />
</tabs>
</TabPane>
</children>
</VBox>
</children>
</AnchorPane>


In my BoxFrameController I have this:

public class BoxFrameController extends MainFrameController {

private Stage stage;
private static final Logger LOGGER = LoggerFactory.getLogger(BoxFrameController.class);


@FXML
public Button createButton;
@FXML
public Button editButton;
@FXML
public Button deleteButton;
@FXML
public Button searchButton;

@FXML
public TextField fromRateTextField, toRateTextField, fromAreaTextField, toAreaTextField, litterTextField;

@FXML
public CheckBox dailyRateCheckBox, areaCheckBox, litterCheckBox, isWindowedCheckBox, isSingleCheckBox;

@FXML
public ImageView WindowedImageView, SingleImageView, horseImageView;

@FXML
public Label fromRateLabel, toRateLabel, fromAreaLabel, toAreaLabel, litterLabel;

@FXML
public TableView tableView;

@FXML
public TableColumn rateColumn, areaColumn, litterColumn;

public void fillTable(ServiceInterface service) throws ServiceException {
tableView.refresh();
try {
List<Box> boxes = service.searchBoxesParameters(0,0,0,"",0,0,false,false);
tableView.setItems(FXCollections.observableArrayList(boxes));
tableView.getSelectionModel().selectFirst();
tableView.refresh();
} catch (ServiceException ex) {
LOGGER.error(ex.toString());
}
}
}


And in my BoxFrame.fxml I have this line (think that should be enough):

<AnchorPane prefHeight="720.0" prefWidth="1001.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sepm.ss17.e1326220.gui.BoxFrameController">


With the debugger I found out that my boxFrameController in
setService()
of the MainFrameController is null and I just can't figure out why...

I'm using Scenebuilder for all of this and I also tried to look for a mistake there, but couldn't find it there aswell, so I was hoping that maybe one of you guys already knows this error.

Answer Source

In the controller you have

@FXML
private BoxFrameController boxFrameController;

but in the FXML you have

<fx:include fx:id="box" source="BoxFrame.fxml" />

The rule for nested controllers is that the controller is injected into a field whose name is the fx:id concatenated with "Controller", so your field should be

@FXML
private BoxFrameController boxController;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download