JavaFX:从第一个窗口设置第二个窗口中的 TextArea 内容时出现空指针异常

问题描述 投票:0回答:1

我正在使用 JavaFX 并创建两个窗口应用程序,一个用于浏览文件,另一个用于显示其内容

问题是当我访问 inputText.setText(filecontent) 时出现空指针异常 正如它所说的 inputText 为 null


public class AppController  {


    private Button BrowseButton;


    @FXML
    private void handleBrowseButtonAction(MouseEvent event) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Choose a File");
        File selectedFile = fileChooser.showOpenDialog(BrowseButton.getScene().getWindow());

        if (selectedFile != null) {
            try {
                String fileContent = Files.readString(selectedFile.toPath());
                openFullScreenStage(fileContent);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    private void openFullScreenStage(String fileContent) {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("editor.fxml"));
            Parent root = loader.load();

            // Accessing the controller of the editor.fxml
            editorController editorController1 = loader.getController();

            // Assuming TextArea in editor.fxml has the fx:id "inputText"
            /*TextArea textArea = editorController1.inputText;*/

            //editorController1.initialize();
           // editorController1.fakeinitialize();

            editorController1.setInputText(fileContent);



                Stage fullScreenStage = new Stage();
                fullScreenStage.setFullScreen(true);
                fullScreenStage.setScene(new Scene(root));
                fullScreenStage.show();

                //System.err.println("TextArea in editor.fxml is null");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

文本区域的控制器


package com.editor.xml_editor;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextArea;

import java.net.URL;
import java.util.ResourceBundle;

public class editorController implements Initializable {
    @FXML
    protected TextArea inputText;


    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        this.inputText.setText("Hello World");
    }

    public void setInputText(String input) {
        inputText.setText(input);
    }

    // Assuming you want to expose this TextArea


    // This TextArea is used internally



}


文本区域的 Fxml


<AnchorPane prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/20" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.editor.xml_editor.editorController">
   <children>
     
      <BorderPane layoutY="26.0" prefHeight="370.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="25.600000000000023">
         
         <center>
            <SplitPane dividerPositions="0.48328877005347587" prefHeight="160.0" prefWidth="200.0" BorderPane.alignment="CENTER">
              <items>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
                     <children>
                        <TextArea id="inputText" layoutX="15.2" layoutY="48.0" prefHeight="322.0" prefWidth="290.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="16.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
                     </children>
                  </AnchorPane>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
                     <children>
                        <TextArea id="outputText" editable="false" layoutX="41.0" layoutY="53.0" prefHeight="322.0" prefWidth="290.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="16.0" AnchorPane.topAnchor="0.0" />
                     </children>
                  </AnchorPane>
              </items>
            </SplitPane>
         </center>
      </BorderPane>
   </children>
</AnchorPane>

我尝试了所有能做的编程

java user-interface javafx textarea fxml
1个回答
0
投票

您的 FXML 文档中的身份属性错误。

您应该使用

fx:id
,而不是
id

这个:

<TextArea id="inputText" . . . 

应该是:

<TextArea fx:id="inputText" . . .

对于 FXML 中定义的其他 id 类似,如

outputText

通过设置

id
,您仅设置 CSS ID,而不是可由加载程序使用
@FXML
注释注入的 FXML 引用。

FXML 参考 并不擅长记录这个主题,但它说:

为元素分配

fx:id
值会在文档的命名空间中创建一个变量,稍后可以通过变量取消引用属性(例如上面显示的“toggleGroup”属性)或在脚本代码中引用该变量,这将在后面的部分中讨论。此外,如果对象的类型定义了“id”属性,则该值也将传递给对象
setId()
方法。

所以

fx:id
还设置
id
(用作 CSS 标识符),除了在命名空间中放置对对象的引用之外,该引用可以由加载器在控制器类或引用中的公共引用上提取和设置用
@FXML
注释进行注释。

请参阅 有关控制器的文档和

@FXML
了解更多信息和示例。

© www.soinside.com 2019 - 2024. All rights reserved.