Java file browser

Soldato
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
I'm sure there must be something in the existing Java libraries that opens up a file browser window so that you can browse to any file on the system and it returns the name of the file plus the location as a string.

Am I right? If so where should I be looking?
 
What about JFIleChooser

Code:
import javax.swing.JFileChooser;

JFileChooser chooser = new JFileChooser();

// gets the result from the chooser
int result = chooser.showOpenDialog(this);
        
switch ( result )
{
     case JFileChooser.APPROVE_OPTION:

     File file = chooser.getSelectedFile();
     String name = file.getName();
     String path = file.getPath();    
     break;
     
     case JFileChooser.CANCEL_OPTION:
     case JFileChooser.ERROR_OPTION:
                break;
            default:
 }

or am I way off?
 
Last edited:
Rossmac said:
What about JFIleChooser

Code:
import javax.swing.JFileChooser;

JFileChooser chooser = new JFileChooser();

// gets the result from the chooser
int result = chooser.showOpenDialog(this);
        
switch ( result )
{
     case JFileChooser.APPROVE_OPTION:

     File file = chooser.getSelectedFile();
     String name = file.getName();
     String path = file.getPath();    
     break;
     
     case JFileChooser.CANCEL_OPTION:
     case JFileChooser.ERROR_OPTION:
                break;
            default:
 }

or am I way off?

Nope that's pretty much how I did it in the end, nice and easy :)
 
Back
Top Bottom