I'm converting a pure JavaFx app, in which the code below worked fine when put all in one class, to a FXML one, where the Stage declaration and the button handler are in separate classes. In the a Controller, I'm trying to implement a method that will allow the user to choose a directory and store it in a variable for later use:
private File sourceFile;
DirectoryChooser sourceDirectoryChooser;
@FXML
private void handleSourceBrowse() {
sourceDirectoryChooser.setTitle("Choose the source folder");
sourceFile = sourceDirectoryChooser.showDialog(theStage);
}
However, "theStage", a Stage which the method requires, only exists(if that's the right terminology) in FolderSyncer4.java:
public class FolderSyncer4 extends Application {
final String FOLDER_SYNCER = "FolderSyncer";
Stage theStage;
@Override
public void start(Stage primaryStage) throws Exception {
theStage = primaryStage;
//TODO do the FXML stuff, hope this works
Parent root = FXMLLoader.load(getClass().getResource("FolderSyncerMainWindow.fxml"));
theStage.setScene(new Scene(root, 685, 550));
theStage.setTitle(FOLDER_SYNCER);
theStage.show();
}
}
How to I get around this? I need to have that method implemented again somehow, but suddenly I can't pass the stage as an argument.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…