我正在尝试将FXML文件包含在mainview.fxml的选项卡中,并且我想使用不同的控制器。我在一年前做过这个并且它有效,但当我再次尝试时,我的其他控制器都是空的,除了主控制器。
这是我的大型机控制器代码:
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);
}
}
这是我的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>
在我的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());
}
}
}
在我的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">
使用调试器,我发现MainFrameController的setService()
中的boxFrameController为null,我无法弄清楚为什么......
我正在使用Scenebuilder进行所有这些,我也试图在那里寻找错误,但在那里找不到它,所以我希望也许你们其中一个人已经知道这个错误。
答案 0 :(得分:2)
在您拥有的控制器中
@FXML
private BoxFrameController boxFrameController;
但在FXML中你有
<fx:include fx:id="box" source="BoxFrame.fxml" />
nested controllers的规则是控制器被注入一个名称为fx:id
并与"Controller"
连接的字段,因此您的字段应为
@FXML
private BoxFrameController boxController;