[英]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. 我正在尝试在mainview.fxml的选项卡中包括FXML文件,对于那些我想使用不同控制器的文件。 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: 这是我的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: 在我的BoxFrameController中,我有这个:
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): 在BoxFrame.fxml中,我有这行代码(认为应该足够了):
<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... 使用调试器,我发现MainFrameController的setService()
中的setService()
为null,我只是想不出原因...
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. 我正在使用Scenebuilder进行所有操作,并且我也尝试在该位置查找错误,但也无法在此找到它,因此我希望也许你们中的一个已经知道此错误。
In the controller you have 在控制器中
@FXML
private BoxFrameController boxFrameController;
but in the FXML you have 但是在FXML中
<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 嵌套控制器的规则是将控制器注入到名称为fx:id
并与"Controller"
串联的字段中,因此您的字段应为
@FXML
private BoxFrameController boxController;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.