【问题标题】:Multiple FXML files with controllers for each file - TextArea not displaying text properly after append多个 FXML 文件,每个文件都有控制器 - 附加后 TextArea 无法正确显示文本
【发布时间】:2019-02-25 10:15:54
【问题描述】:

我想将我的 FXML 分成更小的文件,每个文件都有自己的控制器。在 main 中,我为每个控制器创建实例并访问 textAreaSample 并尝试附加文本。我没有看到文字正在改变。为什么? Alert 正在显示来自此 TextArea 的文本:

alert.setContentText(textAreaSample.getText());

我不知道如何设置所有 fxml 文件和控制器。我应该如何设置所有这些?

这是我的主要“sample.fxml”文件:

<GridPane fx:controller="sample.ControllerSample"
      xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">

    <fx:include fx:id="sending" source="Sending.fxml" GridPane.columnIndex="0" GridPane.rowIndex="0"/>

    <TextArea fx:id="textAreaSample" prefWidth="200" prefHeight="200"
          GridPane.columnIndex="1" GridPane.rowIndex="0" text="Sample">
    </TextArea>
</GridPane>

及其控制器:

public class ControllerSample {

    @FXML
    private TextArea textAreaSample;

    public ControllerSample() {}

    public TextArea getTextAreaSample() {
        return textAreaSample;
    }

    public void setTextAreaSample(TextArea textAreaSample) {
        this.textAreaSample = textAreaSample;
    }
}

现在我有Sending.fxml 文件:

<GridPane fx:controller="sample.ControllerSending"
          xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">

        <fx:include fx:id="sendingPhotos" source="SendingPhotos.fxml" GridPane.columnIndex="0" GridPane.rowIndex="0"/>
</GridPane>

及其控制器:

public class ControllerSending {

    public ControllerSending() {}
}

这里是SendingPhotos.fxml代码:

<TextArea xmlns="http://javafx.com/javafx"
      xmlns:fx="http://javafx.com/fxml"
      fx:controller="sample.ControllerSendingPhotos" fx:id="textAreaSendingPhotos" prefWidth="200" prefHeight="200"
      text="Photos"/>

和控制器:

public class ControllerSendingPhotos {

    @FXML
    private TextArea textAreaSendingPhotos;

    public ControllerSendingPhotos() {}

    public TextArea getTextAreaSendingPhotos() {
        return textAreaSendingPhotos;
    }

    public void setTextAreaSendingPhotos(TextArea textAreaSendingPhotos) {
        this.textAreaSendingPhotos = textAreaSendingPhotos;
    }
}

现在Main.java代码:

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        FXMLLoader loaderSample = new FXMLLoader(getClass().getResource("sample.fxml"));
        FXMLLoader loaderSending = new FXMLLoader(getClass().getResource("Sending.fxml"));
        FXMLLoader loaderSendingPhotos = new FXMLLoader(getClass().getResource("SendingPhotos.fxml"));

        loaderSample.load();
        loaderSending.load();
        loaderSendingPhotos.load();

        ControllerSample controllerSample = (ControllerSample) loaderSample.getController();
        ControllerSending controllerSending = (ControllerSending) loaderSending.getController();
        ControllerSendingPhotos controllerSendingPhotos = (ControllerSendingPhotos) loaderSendingPhotos.getController();

        TextArea textAreaSample = controllerSample.getTextAreaSample();
        textAreaSample.setText("\ndebug textAreaSample\n");

        Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "");

        alert.setContentText(textAreaSample.getText());
        alert.showAndWait();

        //TextArea textAreaSendingPhotos = controllerSendingPhotos.getTextAreaSendingPhotos();
        //textAreaSendingPhotos.appendText("\ndebug textAreaSendingPhotos\n");

        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 800, 400));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

编辑:我在ControllerSample 中用getter 和setter 定义ControllerSending?然后在ControllerSending 我用getter 和setter 定义ControllerSendingPhoto?然后在 Main 中声明 ControllerSendingPhotos 我使用 controllerSample.getControllerSending().getControllerSendingPhotos()?

【问题讨论】:

  • 关键是我没有收到任何错误,没有什么是空的,即使(在检查警报后)textArea 包含附加文本。它只是不在TextArea 中显示。
  • 如果您有嵌套的 FXML,则不应单独加载它们。内部 FXML 会自动与外部 FXML 一起加载。 textArea 可能存在两次。显示一个,另一个是您在代码中签入的那个。
  • 我还以为可以有更多的textAreas。但是这个textAreaSample 放在主FXML 文件中。所以,首先我开始用这个进行测试。
  • Grrr。您加载 sample.fxml 两次。第二次直接在primaryStage.setTitle 之前。这会破坏你之前所做的。

标签: java javafx fxml


【解决方案1】:

要访问嵌套 fxml 中的 textArea,您必须更改控制器:

public class ControllerSample {
    @FXML
    private TextArea textAreaSample;

    @FXML
    private ControllerSending sendingController;

    public ControllerSample() {
    }

    public TextArea getTextAreaSample() {
        return textAreaSample;
    }

    public void setTextAreaSample(TextArea textAreaSample) {
        this.textAreaSample = textAreaSample;
    }

    protected ControllerSending getSendingController() {
        return sendingController;
    }
}

public class ControllerSending {
    @FXML
    private ControllerSendingPhotos sendingPhotosController;

    public ControllerSending() {
    }

    protected ControllerSendingPhotos getSendingPhotosController() {
        return sendingPhotosController;
    }
}

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        FXMLLoader loaderSample = new FXMLLoader(getClass().getResource("sample.fxml"));

        Parent root = loaderSample.load();

        ControllerSample controllerSample = (ControllerSample) loaderSample.getController();

        TextArea textAreaSample = controllerSample.getTextAreaSample();
        textAreaSample.setText("\ndebug textAreaSample\n");

        TextArea textAreaSendingPhotos = controllerSample.getSendingController().getSendingPhotosController()
            .getTextAreaSendingPhotos();
        textAreaSendingPhotos.setText("test test test");

        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 800, 400));
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}

【讨论】:

  • sendingControllercontrollerSending名字有区别吗?
  • 有...controllerSending 对我来说看起来更好,但我得到空指针异常。我在我的项目中看不到任何地方 sendingController 名称,这是任何命名约定吗?
  • 我明白了,我将sending.fxml 中的fx:id 重构为sendingG 并在ControllerSample.java 中出现sendingGController
猜你喜欢
  • 2013-05-28
  • 2020-03-26
  • 2014-08-07
  • 1970-01-01
  • 1970-01-01
  • 2017-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多