JFileChooser open button validation

200 views Asked by At

Im using a filechooser to select a directory, the addFile(below) command part actually adds a directory but this is offtopic.

My issue is that when selecting the directory i can't use the open button unless i write something(anything that doesnt have special characters) in the "file name" text area.

So if i select a folder and click "open" or double click on folder the folder is opened in the filechooser, if i want to confirm the folder unless i write something in the text area nothing happens. If i do write gibberish in the text area and click open the current directory is correctly chosen.

What am i missing? i would like to avoid overriding that validation part if i can...

public class FileCommandVM extends Panel implements ActionListener{

    private ActionListener father;
    private FileHolder fileHolder;
    private JFileChooser  fc;
    private String caption;

    static final String select="SELECT";

    public FileCommandVM(Object object,ActionListener al) {
        caption="add";
        this.setLayout(null);
        fc=new JFileChooser();
        this.fileHolder=(FileHolder)object;
        JFileChooser  fc=new JFileChooser();
        JButton b=new JButton(caption);
        b.addActionListener(this);
        this.add(fc);
        this.add(b);
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fc.setDialogType(JFileChooser.OPEN_DIALOG);
        fc.setAcceptAllFileFilterUsed(false);
        this.setSize(50,30);
        b.setBounds(1,1,19,15); 
        this.father=al;
    }    
    @Override
    public void actionPerformed(ActionEvent e) {
        int res=(fc.showOpenDialog(null));
        this.fileHolder.setFile(fc.getCurrentDirectory());
        if(res==JFileChooser.APPROVE_OPTION){
            ActionEvent add=new ActionEvent(this.fileHolder, 0, "addFile");
            father.actionPerformed(add);
        }
    }
}
0

There are 0 answers