I would like to pop up a window to select a file location when the user launch the software for the first time. I'm new to javafx and I looked for an answer on the web but no success... Thanks in advance
-
Have you had a look at *JavaFX*’s [`FileChooser`](http://docs.oracle.com/javafx/2/ui_controls/file-chooser.htm)? – D. Ataro Jan 20 '17 at 02:05
-
I know about FileChooser. The problem isn't to create the UI to get the file but how to run something at first load. Ex. Alert that would ask if the user would like to select a file or use default location.... – RekTek249 Jan 20 '17 at 02:13
-
A JavaFX [Application](https://docs.oracle.com/javase/8/javafx/api/javafx/application/Application.html) has an `init()` and `start()` method, you can just do the work you need in one of those `init()` for non-UI work and `start()` for UI work (e.g. show a FileChooser, Alert or the main stage). Seems straight-forward and obvious, so I guess I am misunderstanding what your question is. Perhaps Jai understood better and created the appropriate answer. – jewelsea Jan 20 '17 at 09:35
3 Answers
I think I found it.
primaryStage.setOnShowing(event -> {
//Code here
});
It does action on first startup. I don't know if it's the best way to do it but that's how I did it. I already have a file with about 3 lines. I just added a 4th one with a random word and when the user launch the app it check if the word exist in the file. If so, it does nothing. If not, it ask the user to select the folder and if the selection is successful, it write the word.
Firstly, I would want to point out that your phrasing probably isn't clear enough for most people to understand exactly what you need. I am going to assume you have some kind of settings (like default application storage directory) which you need the user to specify at the first time the JAR is run. If the JAR file is run subsequently, it should not prompt for that again and use the settings previously specified.
Typically, when the user runs the JAR file, all the data would be isolated within that session. If the user closes the application and opens the application again, it would behave just like the previous run.
If you need to persist these data or settings, you can use Properties
. This will save data in a separate file. The normal convention is to save it in the same folder as the JAR file, and named as config.properties
.
At the start of the application, you should check if this file exists, if it does not exist, it means that this is the first run. Subsequently, when the user set the data (e.g. file folder), you would save it to the file.
You can find an example here.

- 8,165
- 2
- 21
- 52
-
1Sorry I'm French I tried my best to phrase it... But I did what you said and It's fine now. Thanks – RekTek249 Jan 20 '17 at 21:44
Background
In order to implement a file selector, we can make use of JavaFX’s FileChooser
. This will open a window giving us the opportunity to select a file.
What you’re asking for is for a FileChooser
to open prior to entering the actual application. Let’s have a look at the implementation for something like that!
Implementation
At first, we’re going to need a JavaFX Application
class that will open a window if we were to create a new instance out of it:
public class App extends Application
{
private final File file;
public App(File file)
{
this.file = file;
// Optionally provide ‘launch’ with some arguments
Application.launch();
}
@Override
public void start(final Stage stage)
{
// ...
stage.setScene(new Scene(insertNodeHere));
}
}
As I’m certain you’re already aware of—a class like this will open up a new window. This is the separate application class we’ll be calling once we’ve retrieved a File
using the FileChooser
in our main
class.
In our main
class, we’ll put this:
File file = fileChooser.showOpenDialog(stage);
if (file != null)
new App(file);
This will launch your application if the selected file didn’t turn out as null
.
Moreover
With the implementation above in mind, you can complicate things as much as you feel like. Perhaps you’d like the application to start even if the file were null? In that case, there’s no need for the if
statement.

- 1,711
- 17
- 38
-
1You shouldn't show the FileChooser until after the JavaFX application is launched and the JavaFX application initialized (which will be when the `start()` method of the application is called). You shouldn't launch the application in a constructor as that will create a new instance of the application object (in addition to the one created by the constructor), which is very confusing. – jewelsea Jan 20 '17 at 09:33