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();
}
}
}
};
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With