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

2

6

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!

Ismaelisman answered 15/4, 2014 at 19:32 Comment(1)
Check the new answer!Campus
P
8

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

    }
}
Pavier answered 15/4, 2014 at 20:5 Comment(1)
Can you please upvote my question because I need 15 reputation to upvote others.Ismaelisman
H
0
@Override
public void approveSelection(){
    for(File f : this.getSelectedFiles())
        if(!f.exists()) {
            //Show warning to user
            //if needed: cancelSelection();
            return;
        }              
    super.approveSelection();
}        
Hobie answered 15/4, 2014 at 19:40 Comment(4)
No listener is needed ;).Campus
But I need JFileChooser to stay opened after Approve button is pressedIsmaelisman
Ok, just curious, why do you need to JFileChooser to stay opened?Campus
To make user understand that this file doesn't exist and new one can be chosen.Ismaelisman

© 2022 - 2024 — McMap. All rights reserved.