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!