我有两个控制器FXMLDocumentController
和FXMLOpenedCodeController
。我正在从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;
我删除了@FXML
和private
,以便代码可以正常运行。
答案 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();
}