Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JFileChooser choose empty file

I'm using a JFileChooser as the editor for a JTable cell. I'd like the user to select a valid file using JFileChooser, then when they hit enter, the file path is saved to the cell. This presents a problem if they want to clear the cell. So i want them to clear out the JFileChooser and that will set the cell with the empty string (or null, whichever).

My problem is that if you haven't selected a file, you can't press the Approve button. In my code, "empty!" is never printed. Is there a way to do allow the approve button selected when no file is selected? Here's what I've tried:

JFileChooser component = new JFileChooser(){
        public void approveSelection(){
            File f = getSelectedFile();
            if(f==null){
                System.out.println("empty!");
                return;
            }else{
                if(!f.exists()){
                    System.out.println("does not exist!");
                }else{
                    super.approveSelection();
                }

            }
        }
    };
like image 743
Andrea Avatar asked Aug 27 '11 01:08

Andrea


1 Answers

you may be interested in overidding JFileChooser cancelSelection() method which is called when user cancels file choosing.

maybe it is not intuitive and user friendly to do JTable cell clearing with JFileChooser selection of an empty file name. Better have a small button beside the JTable cell so it clears cell value if user clicks it or any other option to reset cell value and only use JFileChooser for file path changing in the cell.

like image 81
othman Avatar answered Sep 22 '22 16:09

othman