Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I open the JavaFX FileChooser from a controller class?

My problem is that all the examples of using FileChooser requires you to pass in a stage. Only problem is that my UI is defined in an fxml file, which uses a controller class separate from the main stage.

@FXML protected void locateFile(ActionEvent event) {     FileChooser chooser = new FileChooser();     chooser.setTitle("Open File");     chooser.showOpenDialog(???); } 

What do I put at the ??? to make it work? Like I said, I don't have any references to any stages in the controller class, so what do I do?

like image 411
Electric Coffee Avatar asked Aug 25 '14 17:08

Electric Coffee


People also ask

What is FileChooser in JavaFX?

FileChooser class is a part of JavaFX. It is used to invoke file open dialogs for selecting a single file (showOpenDialog), file open dialogs for selecting multiple files (showOpenMultipleDialog) and file save dialogs (showSaveDialog). FileChooser class inherits Object class.

Which interface should be used with controller class of JavaFX?

JavaFX controller works based on MVC(Model-View-Controller) JavaFX MVC can be achieved by FXML (EFF-ects eXtended Markup Language). FXML is an XML based language used to develop the graphical user interfaces for JavaFX applications as in the HTML.


1 Answers

For any node in your scene (for example, the root node; but any node you have injected with @FXML will do), do

chooser.showOpenDialog(node.getScene().getWindow()); 
like image 183
James_D Avatar answered Sep 20 '22 23:09

James_D