JavaFX8 - 带有单独的FXML和控制器的选项卡

时间:2023-02-04 17:04:07

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.

我试图在我的mainview.fxml的选项卡中包含FXML文件,并且我想使用不同的控制器。我在一年前做过这个并且它有效,但是当我再次尝试它时,我的其他控制器都是空的,除了主控制器。

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()中的boxFrameController为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进行所有这些,我也试图在那里寻找错误,但在那里找不到它,所以我希望也许你们其中一个人已经知道这个错误了。

1 个解决方案

#1


2  

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;

#1


2  

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;