DoneEvent

// Create customized chooser
MyFileChooser chooser = new MyFileChooser();

// Set dialog type if not OPEN_DIALOG
chooser.setDialogType(JFileChooser.SAVE_DIALOG);

// Create dialog containing the chooser
final JDialog dialog = chooser.createDialog(frame);

// Add listener for approve and cancel events
chooser.addActionListener(new AbstractAction() {
    public void actionPerformed(ActionEvent evt) {
        JFileChooser chooser = (JFileChooser)evt.getSource();
        if (JFileChooser.APPROVE_SELECTION.equals(evt.getActionCommand())) {
            // Open or Save was clicked

            // Hide dialog
            dialog.setVisible(false);
        } else if (JFileChooser.CANCEL_SELECTION.equals(evt.getActionCommand())) {
            // Cancel was clicked

            // Hide dialog
            dialog.setVisible(false);
        }
    }
});

// Add listener for window closing events
dialog.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
        // Close-dialog icon was clicked

        // Hide dialog
        dialog.setVisible(false);
    }
});

// Show the dialog; wait until dialog is closed
dialog.show();

// This version of JFileChooser simply makes createDialog a public method.
public class MyFileChooser extends JFileChooser {
    public JDialog createDialog(Component parent) throws HeadlessException {
        return super.createDialog(parent);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.