如何在按下批准或取消按钮时阻止JFileChooser关闭? [英] How to stop JFileChooser from being closed when approve or cancel button is pressed?

查看:163
本文介绍了如何在按下批准或取消按钮时阻止JFileChooser关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use method showOpenDialog of JFileChooser to open file.

如何将ActionListener附加到JFileChooser的批准"按钮以及如何停止此对话框 从单击批准"按钮并完成侦听器后关闭.

How to attach ActionListener to Approve button of JFileChooser and how to stop this dialog from closing after Approve button is clicked and listener completed.

现在我有:

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,如果文件不存在,则会弹出错误消息.

Method showOpenFileDialog brings up a dialog and when I press Approve button actionListener is called and if file doesn't exist then error message is popped up.

但是JFileChooser还是要关闭.如果文件不存在,我希望JFileChooser保持打开状态!

But JFileChooser is closing anyway. I want JFileChooser to stay opened if file doesn't exist !

谢谢!

推荐答案

您可以覆盖approveSelection()方法以检查文件是否存在:

You can override the approveSelection() method to check if the file exists:

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

    }
}

这篇关于如何在按下批准或取消按钮时阻止JFileChooser关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆