For example, I want open a DirectoryChooser when clicking on the button:
<VBox fx:controller="com.foo.MyController" xmlns:fx="http://javafx.com/fxml"> <children> <Button text="Click Me!" onAction="#handleButtonAction"/> </children> </VBox>
And the Controller class:
package com.foo; public class MyController { public void handleButtonAction(ActionEvent event) { DirectoryChooser dc = new DirectoryChooser(); File folder = dc.showDialog(null);//I want to put the WIndows here. } }
I want to put the main Window to the ShowDialog so that it will be blocked but how can I access it?
FXML Controller Classes There are two ways to set a controller for an FXML file. The first way to set a controller is to specify it inside the FXML file. The second way is to set an instance of the controller class on the FXMLLoader instance used to load the FXML document.
The base class for all nodes that have children in the scene graph. This class handles all hierarchical scene graph operations, including adding/removing child nodes, marking branches dirty for layout and rendering, picking, bounds calculations, and executing the layout pass on each pulse.
In JavaFX, the Stage can be navigated to by using a node's getScene() method, followed by retrieving the Window using getWindow() . As the JavaFX Stage extends the Window class, this can be cast to Stage. So, getting the Stage from the current controller is incredibly easy.
The <fx:include> tag can be used to include one fxml file into another. The controller of the included fxml can be injected into the controller of the including file just as any other object created by the FXMLLoader . This is done by adding the fx:id attribute to the <fx:include> element.
You can ask any node for the Scene
and then call Scene#getWindow()
.
((Node) event.getTarget()).getScene().getWindow()
From @osvein if this is a handler for a MenuItem
it should be:
((MenuItem) event.getTarget()).getParentPopup().getOwnerWindow()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With