Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get parent Window in FXML Controller?

Tags:

fxml

javafx-2

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?

like image 488
nvcnvn Avatar asked Nov 27 '12 13:11

nvcnvn


People also ask

How do I specify a controller in FXML?

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.

What is JavaFX scene parent?

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.

How do I get Windows JavaFX?

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.

How do I add FXML to another FXML?

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.


1 Answers

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() 
like image 165
Sergey Grinev Avatar answered Sep 20 '22 12:09

Sergey Grinev