Its been a while since my last post, today I gonna show you how easy using file open dialog in java or called JFileChooser to get file path or location by select them. First lets make new Frame and put JTextField and JButton inside of it simply like picture below :

After that put this code inside of your JButton action event :

//File separator (\)
String fileSeparator = System.getProperty("file.separator");
JFileChooser chooser = new JFileChooser();
//set JFileChooser default path
//set JFileChooser title
chooser.setDialogTitle("Pilih Folder");
//use as you need DIRECTORIES_ONLY, FILES_ONLY etc

//false, if you want to view all kind of file type in JFileChooser
//true, if you want to create FileFilter and open only file type you want to be view in JFileChooser

if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
    //write selected path inside JTextField
} else {
    System.out.println("No Selection");

You can also assign and use the selected path into variable and use it directly to open the file. Beside getPath() of course there’s many more option you could choose.

Here is it how is JFileChooser working :

Easy isn’t? Hope the post useful, thanks for reading 😉 .

For info about my newest post :