How to use JFileChooser to find a file location

前端 未结 3 1231
春和景丽
春和景丽 2021-01-16 01:38

Is there a method that i can use to simply find a file location? I\'m trying allow the user to choose a file and open it, but I have to have the JFileChooser just choose the

3条回答
  •  长发绾君心
    2021-01-16 02:18

    The example in the javadoc show show to do this:

    JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter(
        "JPG & GIF Images", "jpg", "gif");
    chooser.setFileFilter(filter);
    int returnVal = chooser.showOpenDialog(parent);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
       System.out.println("You chose to open this file: " +
            chooser.getSelectedFile().getName());
    }
    

    That's what chooser.getSelectedFile() is doing. Take the result of that and pass it to another method.

提交回复
热议问题