5

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();
                }

            }
        }
    };
Andrea
  • 1,057
  • 1
  • 20
  • 49

3 Answers3

4

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.

othman
  • 4,428
  • 6
  • 34
  • 45
  • i had thought about the reset button also. I want to leave the cancel button in place in case the user wants to cancel. Though I guess that can be done with the exit button at the top... – Andrea Aug 27 '11 at 12:11
  • 2
    +1 for the idea to have a separate button that clears the cell. Choosing cancel should really just revert the cell back to the state it was in before the JFileChooser opened. – Eric Hydrick Aug 27 '11 at 17:54
1

When you run the code you have provided, it's not even getting into the approveSelection handler if no file is selected. Internally, there's code to prevent a null selection. This behavior comse from BasicFileChooserUI's ApproveSelectionAction method which has a check that if the filename is null, it returns without calling approveSelection on the chooser, so the JFileChooser never gets a notification that "Accept" was pressed. This is a really long ugly function, so even if you found a way to get access to it, it would be tricky to change the behavior.

Several possible routes of working around this:

1) You could embed the JFileChooser in another component and display the parent component instead. Then you would add both the JFileChooser and your custom button to the same panel and add button handling for the custom button. The custom button could handle user wishing to select nothing. This post may give some hints: JFileChooser embedded in a JPanel

2) You could abuse the "accessory" feature of the JFileChooser. Normally it is "intended" to show a preview of the file type, but since it takes an arbitrary component so there is no reason you couldn't add your own "No File" button there, and set a listener, and close the dialog if that button is pressed and send the notification to your table cell that it should be a blank value. Relatively easy.

3) you could try to hack the JFileChooser dialog to add an extra button next to Save and Cancel. This probably isn't very easy, and you would probably have to do something tricky to find the Cancel button and add another button to its parent. Probably only doable if you're a java expert.

4) you could create some sort of "fake"/special file, that if selected, you make an exception to the normal handling and display a blank filename instead of the real filename.

Community
  • 1
  • 1
Jessica Brown
  • 8,222
  • 7
  • 46
  • 82
  • thanks for the comprehensive analysis. at least I now understand why clicking the approve button did not do anything. – Andrea Aug 29 '11 at 17:10
  • @Andrea For a very similar problem (allowing the user to Clear the file choice) I recently hacked the _Windows_ LAF version of JFileChooser to add a third button. If you are interested I could send you the code. _Windows only_, though you might be able to do similar tricks for Mac etc... – user949300 Aug 16 '12 at 20:15
0

I wonder if you can write a class that extends JFileChooser, and modify the text field so that clearing it doesn't disable the Accept button.

Eric Hydrick
  • 3,467
  • 2
  • 28
  • 41
  • technically, the Accept button is enabled, just nothing happens when i click it. But the concept should work – Andrea Aug 27 '11 at 01:41
  • I don't think this solution would be feasible for no file selected. Its not actually in the JFileChooser class where it is handling and ignoring the button click. – Jessica Brown Aug 27 '11 at 21:28