Reputation: 3419
I want to ask how I can get the view I am showing in my controller.
The reason I want to do this is that a DirectoryChooser
Dialog requires an ownerWindow
in order to work correctly.
Is there any other solution?
DirectoryChooser directoryChooser = new DirectoryChooser();
File file = directoryChooser.showDialog(/*I need a javafx.stage.window here!*/);
Upvotes: 2
Views: 276
Reputation: 36722
Just fetch the current window's reference from any scene graph element that is currently being shown.
element.getScene().getWindow()
For example, if the dialog opens up from the action of a button, you can fetch the window from the button reference. The code would look like :
button.setOnAction(e -> {
DirectoryChooser directoryChooser = new DirectoryChooser();
File file = directoryChooser.showDialog(button.getScene().getWindow());
});
Upvotes: 3
Reputation: 65811
Not suggesting this is the right way of doing this but this is cretainly a way. The arguments against using public static
should be born in mind here.
public class App extends Application {
public static App mainApp;
public static Stage mainStage;
@Override
public void start(Stage stage) throws Exception {
// Remember me.
mainApp = this;
mainStage = stage;
You can then use:
File newLocation = chooser.showDialog(App.mainStage);
Upvotes: 0