Анкерные элементы в Anchor Pane - PullRequest
0 голосов
/ 12 ноября 2018

Я только что сделал эти три файла с помощью Scene Builder:

textfield.fxml:

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" fx:controller="mailbox.TextFieldController" xmlns:fx="http://javafx.com/fxml/1">
    <TextField fx:id="id" layoutX="252.0" layoutY="39.0" prefHeight="27.0" prefWidth="35.0" />
    <TextField fx:id="mitt" layoutX="252.0" layoutY="73.0" />
    <TextField fx:id="dest" layoutX="252.0" layoutY="108.0" />
    <TextField fx:id="oggetto" layoutX="252.0" layoutY="144.0" />
    <TextField fx:id="data" layoutX="308.0" layoutY="39.0" prefHeight="27.0" prefWidth="110.0" />
</AnchorPane>

textarea.fxml:

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" fx:controller="mailbox.TextAreaController" xmlns:fx="http://javafx.com/fxml/1">
        <TextArea id="textarea" editable="false" layoutX="240.0" layoutY="200.0" prefHeight="200.0" prefWidth="360.0" />
</AnchorPane>

lista.fxml:

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" fx:controller="mailbox.ListController" xmlns:fx="http://javafx.com/fxml/1">
    <ListView id="listView" layoutY="29.0" prefHeight="371.0" prefWidth="240.0" />
</AnchorPane>

Теперь я добавил элементы, передав их в конструктор Anchor Pane и с помощью метода AddAll, но это не сработало:

public void start(Stage stage) throws Exception {

    FXMLLoader listLoader = new FXMLLoader(getClass().getResource("lista.fxml"));
    ListController listController = listLoader.getController();

    FXMLLoader textareaLoader = new FXMLLoader(getClass().getResource("textarea.fxml"));
    TextAreaController textareaController = textareaLoader.getController();

    FXMLLoader fieldLoader = new FXMLLoader(getClass().getResource("textfield.fxml"));
    TextFieldController fieldController = fieldLoader.getController();

    AnchorPane root = new AnchorPane(listLoader.load(), textareaLoader.load(), fieldLoader.load());
    //root.getChildren().addAll(listLoader.load(), textareaLoader.load(), fieldLoader.load());

    DataModel model = new DataModel();
    listController.initModel(model);
    textareaController.initModel(model);

    Scene scene = new Scene(root, 400, 600);
    stage.setScene(scene);
    stage.show();
}

Это ошибка:

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 com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    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.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
    at mailbox.MailBox.start(MailBox.java:38)
    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)
Exception running application mailbox.MailBox

1 Ответ

0 голосов
/ 12 ноября 2018

Контроллеры, созданные на основе атрибута fx:controller в формате fxml, доступны только после вызова load.Вам нужно сделать getController вызовы после создания AnchorPane по этой причине:

FXMLLoader listLoader = new FXMLLoader(getClass().getResource("lista.fxml"));
FXMLLoader textareaLoader = new FXMLLoader(getClass().getResource("textarea.fxml"));
FXMLLoader fieldLoader = new FXMLLoader(getClass().getResource("textfield.fxml"));

AnchorPane root = new AnchorPane(listLoader.load(), textareaLoader.load(), fieldLoader.load());

ListController listController = listLoader.getController();
TextAreaController textareaController = textareaLoader.getController();
TextFieldController fieldController = fieldLoader.getController();

DataModel model = new DataModel();
listController.initModel(model);
textareaController.initModel(model);

...
...