How to open a popup

Right now I have a path to a set of classes, but I want the open file to open, and the user chooses which file to open. I tried JFileChooser but have not been successful so far. Here is my code:

public static void main(String[] args) throws IOException { JFileChooser chooser = new JFileChooser(); int returnValue = chooser.showOpenDialog( null ) ; if( returnValue == JFileChooser.APPROVE_OPTION ) { File file = chooser.getSelectedFile() ; } // I don't want this to be hard-coded: String filePath = "/Users/Bill/Desktop/hello.txt"; 

How can I do it?

+4
source share
1 answer

I think the problem is in the File file .

Try declaring file outside the if block.

  File file = null; if( returnValue == JFileChooser.APPROVE_OPTION ) { file = chooser.getSelectedFile() ; } if(file != null) { String filePath = file.getPath(); } 
+6
source

All Articles