简体   繁体   中英

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

            }
        }
    };

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.

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.

我想知道您是否可以编写一个扩展JFileChooser的类,并修改文本字段,以便清除它不会禁用“接受”按钮。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM