【问题标题】:How to fetch a Path Object from FileChooser JavaFX如何从 FileChooser JavaFX 获取路径对象
【发布时间】:2015-07-02 11:58:13
【问题描述】:

我正在尝试获取这样的路径对象:

private Path file;
private String fileContent;
private Parent root;

@FXML
public void handleOpenFileAction(ActionEvent event) {       
    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Open a File");
    this.file = Paths.get(fileChooser.showOpenDialog(new Stage()).toURI());

    try {
        List<String> lines = Files.readAllLines(this.file, Charset.defaultCharset());

        EditorController editorController = new EditorController();
        editorController.openEditor(lines);

    } catch(IOException ex) {
        System.out.println(ex);
    }
}

但是,当我尝试在 EditorController 类中的另一个方法中输出字符串列表时,我得到了 NullPointerException,如下所示:

@FXML
public TextArea textareaContent;

public Parent root;

public void openEditor(List<String> lines) throws IOException {
    this.root = FXMLLoader.load(getClass().getResource("/com/HassanAlthaf/Editor.fxml"));

    Scene scene = new Scene(this.root);

    Stage stage = new Stage();

    stage.setScene(scene);
    stage.setTitle("Editting File");
    for(String line : lines) {
        this.textareaContent.appendText(line + "\n");
    }
    stage.show();
}

这正是我得到的:http://pastebin.com/QtzQ9RVZ

EditorController.java:40 是这个代码:this.textareaContent.appendText(line + "\n");

TextEditorController.java:38 是这个代码:editorController.openEditor(lines);

如何正确获取它,然后在我的 TextArea 上显示它?请注意,我想使用java.nio 而不是java.io

【问题讨论】:

    标签: java javafx filechooser


    【解决方案1】:

    这与您获取文件的方式无关,问题是您有两个不同的EditorController 实例。当您执行FXMLLoader.load(...) 时,FXMLLoader 会为您创建控制器类的实例并填充@FXML 注释字段。所以那个实例已经初始化了textAreaContent,但是你用new EditorController()创建的实例(以及你调用openEditor)没有。

    像这样重构它:

    编辑器控制器:

    @FXML
    public TextArea textareaContent;
    
    @FXML
    private Parent root;
    
    public void openEditor(List<String> lines) throws IOException {
        Scene scene = new Scene(this.root);
    
        Stage stage = new Stage();
    
        stage.setScene(scene);
        stage.setTitle("Editting File");
        for(String line : lines) {
            this.textareaContent.appendText(line + "\n");
        }
        stage.show();
    }
    

    并将fx:id="root" 属性添加到Editor.fxml 的根元素。

    那就做吧

    @FXML
    public void handleOpenFileAction(ActionEvent event) {       
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Open a File");
        this.file = Paths.get(fileChooser.showOpenDialog(new Stage()).toURI());
        // note the slightly cleaner version:
        // this.file = fileChooser.showOpenDialog(new Stage()).toPath();
    
    
        try {
            List<String> lines = Files.readAllLines(this.file, Charset.defaultCharset());
    
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/HassanAlthaf/Editor.fxml"));
            Parent root = loader.load();
            EditorController editorController = loader.getController();
            editorController.openEditor(lines);
    
        } catch(IOException ex) {
            System.out.println(ex);
        }
    }
    

    【讨论】:

    • Editor.fxml的根元素是什么意思?
    猜你喜欢
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多