如何防止在按下“确认”或“取消”按钮时关闭JFileChooser?

6

我使用JFileChooser的showOpenDialog方法来打开文件。

如何将ActionListener附加到JFileChooser的“确定”按钮上,并在监听器完成后停止此对话框关闭。

目前我的代码:

public class MainFileChooser extends JFileChooser {

    private FileFilter plainFilter;

    public MainFileChooser() {

        super.setMultiSelectionEnabled(true);
        super.setAcceptAllFileFilterUsed(false);

        plainFilter = new PlainFilter();
        }

public int showOpenFileDialog() {
        ActionListener actionListener = null;
        // JDialog openFileDialog = super.createDialog(getParent());
        super.addActionListener(actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                File[] selectedFiles = MainFileChooser.this.getSelectedFiles();
                for (File file : selectedFiles) {
                    if (!file.exists()) {
                        JOptionPane.showMessageDialog(getParent(), file.getName() + " does not exist!",
                                "File is not found", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        });
        super.setFileFilter(plainFilter);
        int userOption = super.showOpenDialog(MainFrame.getInstance().getMainFrame());
        super.removeActionListener(actionListener);
        return userOption;

    }

方法showOpenFileDialog会弹出一个对话框,当我点击“确认”按钮时,会调用actionListener,如果文件不存在,则会弹出错误消息。

但是JFileChooser无论如何都会关闭。如果文件不存在,我希望JFileChooser保持打开状态!

谢谢!


检查新答案! - Julián Chamalé
2个回答

8
你可以重写approveSelection()方法来检查文件是否存在:
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;

public class FileChooserSave
{
    public static void main(String[] args)
    {
        final JFileChooser chooser = new JFileChooser( new File(".") )
        {
            public void approveSelection()
            {
                if (getSelectedFile().exists())
                {
                    super.approveSelection();
                }
                else
                    System.out.println("File doesn't exist");
            }
        };

        chooser.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                System.out.println(e);
            }
        });

        chooser.setSelectedFile( new File("something.txt") );
        int returnVal = chooser.showSaveDialog(null);


        if(returnVal == JFileChooser.APPROVE_OPTION)
        {
           System.out.println(chooser.getSelectedFile() );
        }

    }
}

1
你能否给我的问题点个赞,因为我需要15个声望才能给别人点赞。 - Volodymyr Levytskyi

0
@Override
public void approveSelection(){
    for(File f : this.getSelectedFiles())
        if(!f.exists()) {
            //Show warning to user
            //if needed: cancelSelection();
            return;
        }              
    super.approveSelection();
}        

不需要监听器 ;)。 - Julián Chamalé
但是我需要JFileChooser在单击“确认”按钮后保持打开状态。 - Volodymyr Levytskyi
好的,只是好奇,你为什么需要让JFileChooser保持打开状态? - Julián Chamalé
让用户明白该文件不存在,可以选择新的文件。 - Volodymyr Levytskyi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接