从另一个控制器JavaFX访问一个控制器中的textarea

时间:2016-08-12 17:13:41

标签: javafx scenebuilder

我有两个控制器FXMLDocumentControllerFXMLOpenedCodeController。我正在从FXMLDocumentController读取.txt文件的内容,我希望将该文本放在FXMLOpenedCodeController的文本区域中。代码正在运行并且从FXMLDocumentController读取得很好但是当打开FXMLOpenedCodeController的窗口时,.txt内容中的读取内容在textarea中不可见。我的system.out.println显示String mine有内容,但它没有显示在FXMLOpenedCodeController的textarea中。请帮助任何人。谢谢。

FXMLDocumentController代码

public class FXMLDocumentController implements Initializable {

    @FXML
    private MenuItem open;

    @FXML
    private MenuItem about;

    @Override
    public void initialize(URL url, ResourceBundle rb) {

        open.setOnAction(new EventHandler<ActionEvent>(){
            @Override
                    public void handle (ActionEvent event){
                try {
                    showSingleFileChooser();
                } catch (IOException ex) {
                    Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
                }
                    }            
        });
    }
    private void showSingleFileChooser() throws IOException {
        //Stage s = new Stage();        
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("ZEBRA file open...");
        FileChooser.ExtensionFilter exfil = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
        fileChooser.getExtensionFilters().add(exfil);
        File selectedFile = fileChooser.showOpenDialog(stage);
        if(selectedFile != null){
            FXMLLoader fxmlLoader = new FXMLLoader();
            fxmlLoader.setLocation(getClass().getResource("FXMLOpenedCode.fxml"));
            AnchorPane frame = (AnchorPane) fxmlLoader.load();
            FXMLOpenedCodeController c = fxmlLoader.getController();
            //c.codeExecute = codeExecute;
            c.codeExecute.appendText(readFile(selectedFile));
            String mine;
            mine = readFile(selectedFile);
            //c.codeExecute.appendText(mine);
            System.out.println(mine);
            Parent root = FXMLLoader.load(getClass().getResource("FXMLOpenedCode.fxml"));
            Scene scene = new Scene(root);
            stage4.initModality(Modality.APPLICATION_MODAL);
            stage4.setTitle("Compile Code");
            stage4.setScene(scene);
            stage4.show();
        }   
    }

    private void newWindow() throws IOException{
        Parent root = FXMLLoader.load(getClass().getResource("FXMLNew.fxml"));
        Scene scene = new Scene(root);
        stage3.initModality(Modality.APPLICATION_MODAL);
        stage3.setTitle("Enter code to run here");
        stage3.setScene(scene);
        stage3.show();
    }

    private String readFile(File selectedFile) throws FileNotFoundException, IOException {
        StringBuilder content = new StringBuilder();
        BufferedReader buffRead = null;
        buffRead = new BufferedReader(new FileReader(selectedFile));
        String text;
        while((text = buffRead.readLine())!=null){
            content.append(text);
        }
        return content.toString();
    }
}

并在FXMLOpenedCodeController中有一个public TextArea codeExecute;我删除了@FXMLprivate,以便代码可以正常运行。

1 个答案:

答案 0 :(得分:1)

您正在加载FXMLOpenedCode.fxml两次。您将文本放在第一次加载它的文本区域中,然后显示第二次加载它时获得的UI。因此,显然,您没有看到文本被设置为错误的文本区域。

只需加载FXML文件一次:

if(selectedFile != null){
    FXMLLoader fxmlLoader = new FXMLLoader();
    fxmlLoader.setLocation(getClass().getResource("FXMLOpenedCode.fxml"));
    AnchorPane frame = (AnchorPane) fxmlLoader.load();
    FXMLOpenedCodeController c = fxmlLoader.getController();
    //c.codeExecute = codeExecute;
    String mine;
    mine = readFile(selectedFile);
    c.codeExecute.appendText(mine);
    System.out.println(mine);

    Scene scene = new Scene(frame);
    stage4.initModality(Modality.APPLICATION_MODAL);
    stage4.setTitle("Compile Code");
    stage4.setScene(scene);
    stage4.show();
}