3

Is there some simple way to choose a file path in Java? I've been searching around, and JFileChooser keeps coming up, but that's already too excessive for what I want now, as it seems to require making a whole GUI just for that. I'll do it if need be, but is there a simpler way to get the file path?

I'm wondering if there's something like a JOptionPane dialog box to search for the file path.

1
  • 1
    the file chooser is actually very easy to implement. You don't need to build a whole UI around it. You just have to open it, get the file selected by the user, and then pass it on to your methods. You can even have validation within the panel. Commented Jun 17, 2015 at 14:23

3 Answers 3

2

When you have no surrounding UI, you can simply use this (based on the Answer from Valentin Montmirail)

public static void main( String[] args ) throws Exception
{
    JFileChooser fileChooser = new JFileChooser();
    int returnValue = fileChooser.showOpenDialog( null );

    switch ( returnValue )
    {
    case JFileChooser.APPROVE_OPTION:
        System.out.println( "chosen file: " + fileChooser.getSelectedFile() );
        break;
    case JFileChooser.CANCEL_OPTION:
        System.out.println( "canceled" );
    default:
        break;
    }
}
1
  • Thanks, this is what I was looking for. What threw me was the 'null' in the showOpenDialog. None of the other online tutorials said that you could put in a null if you don't have a surrounding UI. Commented Jun 17, 2015 at 15:51
1

Here is the simpliest way to choose a file path in Java :

public void actionPerformed(ActionEvent e) {
    //Handle open button action.
    if (e.getSource() == openButton) {
        int returnVal = fc.showOpenDialog(YourClass.this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            //This is where a real application would open the file.
            log.append("Opening: " + file.getName() + "." + newline);
        } else {
            log.append("Open command cancelled by user." + newline);
        }
   } ...
}

You can plug this actionPerformed to a button for example, and that's it. Your button will open a GUI to select a file, and if the user select the file JFileChooser.APPROVE_OPTION, then you can perform the action that you want (here only logging what was opened)

See the oracle documentation (https://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html) if you want to do something else (not binding a button ?) or something more complex (filter for some extensions only ? ...)

0

JFileChooser is not that complicated if you only need to choose a file.

public class TestFileChooser extends JFrame {
    public void showFileChooser() {
        JFileChooser fileChooser = new JFileChooser();
        int result = fileChooser.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            System.out.println("Selected file: " + selectedFile.getAbsolutePath());
        }
    }
    public static void main(String args[]) {
        new TestFileChooser().showFileChooser();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.