Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop JFileChooser from being closed when approve or cancel button is pressed?

I use method showOpenDialog of JFileChooser to open file.

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.

For now I have :

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;

    }

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.

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

Thank you!

like image 618
Volodymyr Levytskyi Avatar asked Apr 15 '14 19:04

Volodymyr Levytskyi


1 Answers

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

    }
}
like image 61
camickr Avatar answered Oct 18 '22 15:10

camickr