2

The scenario is, when a radiobutton is selected, I open a JFileChooser to select a DIRECTORY where SHOULD BE some files. I'm trying to show an error message and I want to show again the directory chooser. This is the code (the function I call when radiobutton changes):

private void JFileChooserOpen(java.awt.event.ActionEvent evt) {
    fileChooser.setCurrentDirectory(new java.io.File("."));
    fileChooser = new JFileChooser(); 
    fileChooser.setDialogTitle("Select a directory");
    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

    fileChooser.setAcceptAllFileFilterUsed(false);

    int result = fileChooser.showOpenDialog(fileChooser);
    if (result == JFileChooser.APPROVE_OPTION) {
// here I'm calling a function that searches for specific files. 
// true these files are found, false, they are not.
        if (checkTheDir(fileChooser.getSelectedFile()))
        {
// assigning the path to a label
                thePath.setText(fileChooser.getSelectedFile().toString());
        }
        else
        {
// file not found
            JOptionPane.showMessageDialog(null, "File GuiRap not found!", 
                    "Controlla meglio", JOptionPane.WARNING_MESSAGE);
// what should I do, here, to open again the file dialog window?
// here I'm calling again this function, but surely is not a good practice!
            JFileChooserOpen(evt);
        }
// cancel button changes a radiobutton
    } else if (result == JFileChooser.CANCEL_OPTION) {
        rbnParams.setSelected(true);
    }
} 

Many thanks! F.

alterfox
  • 1,675
  • 3
  • 22
  • 37
Francesco
  • 55
  • 7

3 Answers3

5

When creating JFileChooser, override its approveSelection method (documentation).

JFileChooser fileChooser = new JFileChooser() {
    @Override
    public void approveSelection() {
        // test your condition here
        if (checkTheDir(getSelectedFile())
            super.approveSelection();
        else
            JOptionPane.showMessageDialog(null, "File GuiRap not found!", 
                "Controlla meglio", JOptionPane.WARNING_MESSAGE);                
    }
}

This way, file chooser doesn't close and reopen, but stays open until required condition is met (or cancel performed).

Also, you should provide a parentComponent for that JOptionPane, instead of giving it null.

alterfox
  • 1,675
  • 3
  • 22
  • 37
0

use a do { } while loop around your JFileChooser & JOptionPane, and instead of an error message, display a message like "Necessary file not found. Would you like to select a different directory?", use JOptionPane.YES_NO_OPTION and quit if they say no

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • thank you, but this is not far for calling recursively the function. and I think I can have stack error problems... – Francesco Nov 07 '14 at 14:47
0

Try the next:

private void JFileChooserOpen(java.awt.event.ActionEvent evt) {
    fileChooser = new JFileChooser(); 
    fileChooser.setDialogTitle("Select a directory");
    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    fileChooser.setAcceptAllFileFilterUsed(false);
    boolean valid = false;
    do {
        fileChooser.setCurrentDirectory(new java.io.File("."));
        if (fileChooser.showOpenDialog(fileChooser) == JFileChooser.APPROVE_OPTION) {
            valid = checkTheDir(fileChooser.getSelectedFile());
            if (valid) {
                thePath.setText(fileChooser.getSelectedFile().toString());
            } else {
                JOptionPane.showMessageDialog(null, "File GuiRap not found!", 
                    "Controlla meglio", JOptionPane.WARNING_MESSAGE);
            }
        } else {
            rbnParams.setSelected(true);
            valid = true;
        }
    } while(!valid);
} 
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148