Question

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!

Was it helpful?

Solution

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

    }
}

OTHER TIPS

@Override
public void approveSelection(){
    for(File f : this.getSelectedFiles())
        if(!f.exists()) {
            //Show warning to user
            //if needed: cancelSelection();
            return;
        }              
    super.approveSelection();
}        
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top