FileChooserDemo.java

Download FileChooserDemo.java

 1: // Simple demo of using a swing file open dialog.
 2: // Here we try for the native Look-and-Feel (LaF), if possible.
 3: // Written 2/2013 by Wayne Pollock, Tampa Florida USA
 4: 
 5: import java.io.File;
 6: import javax.swing.*;
 7: 
 8: public class FileChooserDemo {
 9:     public static void main ( String [] args ) {
10:         SwingUtilities.invokeLater( new Runnable() {
11:             public void run () {
12:                 try {
13:                     UIManager.setLookAndFeel(
14:                         UIManager.getSystemLookAndFeelClassName() );
15:                 } catch (Exception e) {} // Too bad; use Java's default LaF
16: 
17:                 JFileChooser fileChooser = new JFileChooser( "." );
18:                 int status = fileChooser.showOpenDialog( null );
19:                 if ( status == JFileChooser.APPROVE_OPTION ) {
20:                     File selectedFile = fileChooser.getSelectedFile();
21:                     System.out.println( "Selected: " + selectedFile.getParent()
22:                        + " --- " + selectedFile.getName() );
23:                 }
24:                 System.exit( 0 );
25:             }
26:         } );
27:     }
28: }