简体   繁体   English

JFileChooser选择空文件

[英]JFileChooser choose empty file

I'm using a JFileChooser as the editor for a JTable cell. 我正在使用JFileChooser作为JTable单元的编辑器。 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. 我希望用户使用JFileChooser选择一个有效文件,然后当他们按Enter键时,文件路径将保存到该单元格中。 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). 因此,我希望他们清除JFileChooser并将其设置为带有空字符串的单元格(或null,以任何一个为准)。

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. 您可能有兴趣覆盖JFileChooser cancelSelection()方法,该方法在用户取消文件选择时调用。

maybe it is not intuitive and user friendly to do JTable cell clearing with JFileChooser selection of an empty file name. 使用JFileChooser选择空文件名来进行JTable单元清除可能不直观且用户友好。 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. 最好在JTable单元格旁边有一个小按钮,以便在用户单击它或其他任何选项以重置单元格值时清除单元格值,并且仅使用JFileChooser更改单元格中的文件路径。

When you run the code you have provided, it's not even getting into the approveSelection handler if no file is selected. 当您运行提供的代码时,如果未选择文件,则甚至不会进入approveSelection处理程序。 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. 此行为来自BasicFileChooserUI的ApproveSelectionAction方法,该方法检查文件名是否为null,是否返回而不调用选择器上的approveSelection,因此JFileChooser永远不会收到有关已按下“接受”的通知。 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. 1)您可以将JFileChooser嵌入另一个组件中,而显示父组件。 Then you would add both the JFileChooser and your custom button to the same panel and add button handling for the custom button. 然后,您将JFileChooser和自定义按钮都添加到同一面板中,并为自定义按钮添加按钮处理。 The custom button could handle user wishing to select nothing. 自定义按钮可以处理希望不选择任何内容的用户。 This post may give some hints: JFileChooser embedded in a JPanel 这篇文章可能会提供一些提示: 嵌入在JPanel中的JFileChooser

2) You could abuse the "accessory" feature of the JFileChooser. 2)您可能会滥用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. 3)您可以尝试修改JFileChooser对话框,在“保存和取消”旁边添加一个额外的按钮。 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. 仅当您是Java专家时才可行。

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. 4)您可以创建某种“伪” /特殊文件,如果选中该文件,则会对常规处理方式造成例外,并显示空白文件名而不是真实文件名。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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