how-to

Dialogs

How to use JFileChooser for open/save dialog

For working with JFileChooser in Java you need use this include:

import javax.swing.*;

Javax.swing.JFileChooser is used for file(s) selection, especially for open/save dialog.

Simplest way of using this class is this:

JFileChooser fc = new JFileChooser();

/* -- check if user select file or cancel dialog */

if ( fc.showOpenDialog( fc ) == JFileChooser.APPROVE_OPTION )
  System.out.println( fc.getSelectedFile().getAbsolutePath() );
else
  System.out.println( "No selection." );

When you click on "Open" button the output will be:

C:\bmp\ANIM\globe8.bmp

For save dialog use method Javax.swing.JFileChooser.showSaveDialog().