[英]JavaFX 8 - Tabpanes and tabs with separate FXML and controllers for each tab
[英]JavaFX8 - Tabs with separate FXML and controllers
我正在嘗試在mainview.fxml的選項卡中包括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()
中的setService()
為null,我只是想不出原因...
我正在使用Scenebuilder進行所有操作,並且我也嘗試在該位置查找錯誤,但也無法在此找到它,因此我希望也許你們中的一個已經知道此錯誤。
在控制器中
@FXML
private BoxFrameController boxFrameController;
但是在FXML中
<fx:include fx:id="box" source="BoxFrame.fxml" />
嵌套控制器的規則是將控制器注入到名稱為fx:id
並與"Controller"
串聯的字段中,因此您的字段應為
@FXML
private BoxFrameController boxController;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.