在JavaFX 8中获取<fx:include>控制器的实例

时间:2018-12-16 01:44:26

标签: javafx javafx-8 fxml

我的问题很简单,但是我仍然不知道为什么它不起作用。

我有启动器文件加载数据库和主控制器,并且主控制器正在为链接的fxml文件(使用fx:include)之一启动另一个控制器。

它确实会启动,但是我无法访问从mainController检索到的invoiceTab invoiceDataBaseContoller的正确实例。

我了解如何从主要加载的fxml中获取控制器,但是我认为我不知道如何检索子控制器。

mainController类

package main;

import javafx.fxml.FXML;
import javafx.scene.layout.StackPane;

public class mainController  {
    @FXML private StackPane mainStack;
     @FXML private invoiceDataBaseController invoiceTab;
    public invoiceDataBaseController getInvoiceController(){
        return invoiceTab;
    }

    @FXML private void initialize() {
        invoiceTab.injectMainController(this);
    }
}

main.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.StackPane?>

<StackPane fx:id="root" focusTraversable="true" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.mainController">
   <children>
      <TabPane prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE">
        <tabs>
          <Tab text="Invoices">
               <content>
               <fx:include fx:id="mainStack" source="invoiceTab.fxml"  id="invoiceTab" />
               </content>
          </Tab>
        </tabs>
      </TabPane>
   </children>
</StackPane>

主类代码:

    public void starter(Stage stage) {
    try {
        dataBase = new sqlitedb();

        indexStart = dataBase.getInvoiceCount() - 99;



        FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));

        Alert alert = new Alert(Alert.AlertType.INFORMATION, "Loading data..");
        Loading(alert);
        root = loader.load();
        scene = new Scene(root);
        mController = loader.getController(); //mainController

        invoicesController = mController.getInvoiceController();
        invoicesController.setStageAndListeners(stage, this, 
                           dataBase.listInvoices(indexStart));



        stage.setTitle("");
        stage.setScene(scene);


        stage.show();
        alert.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

invoicetab.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<StackPane fx:id="mainStack" prefHeight="432.0" prefWidth="573.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.invoiceDataBaseController">
    <children>
        <VBox fx:id="tableViewContainer" alignment="BOTTOM_CENTER" prefHeight="200.0" prefWidth="100.0">
            <children>
                <Label alignment="CENTER" prefHeight="124.0" prefWidth="583.0" text="Invoicing Index">
                    <font>
                        <Font name="Candara" size="85.0" />
                    </font>
                </Label>
                <HBox alignment="BOTTOM_LEFT" prefHeight="53.0" prefWidth="600.0">
                    <children>
                        <TextField fx:id="searchField" maxWidth="-Infinity" prefHeight="23.0" prefWidth="267.0" styleClass="field" HBox.hgrow="NEVER" />
                        <ChoiceBox fx:id="choice" prefHeight="25.0" prefWidth="109.0" />
                        <Button id="confirm" fx:id="createButton" mnemonicParsing="false" text="new" />
                        <Button id="confirm" fx:id="confirmButton" mnemonicParsing="false" text="Confirm" />
                        <Button fx:id="back" mnemonicParsing="false" text="&lt;" />
                        <Button fx:id="next" mnemonicParsing="false" prefHeight="25.0" prefWidth="29.0" text="&gt;" />
                    </children>
                </HBox>
            </children>
        </VBox>
    </children>
</StackPane>

如果需要,错误:

javafx.fxml.LoadException: 
/F:/Java/invoicingSystem%20V3/out/production/invoicingSystem%20V3/main/main.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2571)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
    at main.main.starter(main.java:103)
    at main.main.start(main.java:75)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2566)
    ... 13 more
Caused by: java.lang.NullPointerException
    at main.mainController.initialize(mainController.java:14)

0 个答案:

没有答案