user1907700
user1907700

Reputation:

JFileChooser not showing up

I have a method that takes a txt file as an input. I used to use string by typing the direct path to the file. But it became burdensome whenever I tried to use different file for an input. I try implementing JFileChooser but with no luck.

This is the code, but nothing happening.

public static JFileChooser choose;
File directory = new File("B:\\");
choose = new JFileChooser(directory);
choose.setVisible(true);        
File openFile = choose.getSelectedFile();

FileReader fR = new FileReader(openFile);
BufferedReader br = new BufferedReader(fR);

Upvotes: 4

Views: 10360

Answers (4)

0ct0ber
0ct0ber

Reputation: 21

I personally found that the first dialog would show, but subsequent dialogs wouldn't show. I fixed it by reusing the same JFileChooser with this code.

JFileChooser jfc = new JFileChooser();
File jar = selectFile(jfc, "Select jar to append to");
File append = selectFile(jfc, "Select file to append");

//When done, remove the window
jfc.setVisible(false);

public static File selectFile(JFileChooser jfc, String msg) {
    if (!jfc.isVisible()) {
        jfc.setVisible(true);
        jfc.requestFocus();
    }

    int returncode = jfc.showDialog(null, msg);
    if (returncode == JFileChooser.APPROVE_OPTION) return jfc.getSelectedFile();
    return null;
}

Upvotes: 2

David Kroukamp
David Kroukamp

Reputation: 36423

As per Java tutorial on How to Use File Choosers:

Bringing up a standard open dialog requires only two lines of code:

//Create a file chooser
final JFileChooser fc = new JFileChooser();
...
//In response to a button click:
int returnVal = fc.showOpenDialog(aComponent);

The argument to the showOpenDialog method specifies the parent component for the dialog. The parent component affects the position of the dialog and the frame that the dialog depends on.

Note as per docs it can also be:

int returnVal = fc.showOpenDialog(null);

If the parent is null, then the dialog depends on no visible window, and it's placed in a look-and-feel-dependent position such as the center of the screen.

  Also have a read on Concurrency in Swing if you haven't already.

Upvotes: 6

Emerson Moretto
Emerson Moretto

Reputation: 131

No blocking code (as David Kroukamp suggest). It solves "not showing up" problem.

Runnable r = new Runnable() {

@Override
public void run() {
    JFileChooser jfc = new JFileChooser();
    jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    if( jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION ){
        selected = jfc.getSelectedFile();
    }
}
}
SwingUtilities.invokeLater(r);

Upvotes: 2

Alex
Alex

Reputation: 3181

For JFileChoosers, you're supposed to call objectName.showOpenDialog(Component parent) or objectName.showOpenDialog(Component parent). These methods will return an integer, which you can use to compare to the static constants set in JFileChooser to determine whether the user clicked cancel or open/save. You then use getSelectedFile() to retrieve the file that the user has selected.

Ex (There might be small errors):

class Example {
    public static void main(String[] args) {
        JFileChooser jfc = new JFileChooser();
        File selected;

        if (jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            selected = jfc.getSelectedFile();
        }
    }
}

The Java API is a great resource for figuring out what objects can do what, and how. Here's the page for JFileChoosers

The API pages are usually found when you Google the object name. They're usually the first ones that come up as a result as well.

Upvotes: 0

Related Questions