Home » Java » java – JFrame is disabled after closing JFileChooser and JDialog-Exceptionshub

java – JFrame is disabled after closing JFileChooser and JDialog-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I really need your help, cause I’m struggling with it. I’ve created a JFrame in which I can open up a JDialog for e.g. changing settings. In the JDialog there is a button for starting a JFileChooser. I’m able to choose a file and everything works fine. But if I’m just closing the JFileChooser AND the JDialog, the JFrame will disable and minimize.

Does anyone know how to fix that?

Building JFrame:

frame = new JFrame("My first JFrame");

frame.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e) {
        closeWindow();
    }
});

frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Cancel");
frame.getRootPane().getActionMap().put("Cancel", new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
        closeWindow();
    }
});

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

[...]

frame.getContentPane().setLayout(null);

frame.setVisible(true);

Building JDialog:

final EditColumnsDialog editColumnsDialog = new EditColumnsDialog(frame, ...);
editColumnsDialog.editPicPath();

...

class EditColumnsDialog extends JDialog {

EditColumnsDialog(final JFrame owner, ...) throws Exception {
    super(owner, owner.getTitle());
    [...]
}

...

protected void editPicPath() {
    [...]

    JButton searchButton = new JButton("Search");
    searchButton.setVisible(true);
    searchButton.addActionListener(e -> {
        File folder = WindowBuilder.fileChooser(JFileChooser.DIRECTORIES_ONLY, picPath.getText());
        if (folder != null) {
            picPath.setText(folder.getAbsolutePath());
        }
    });

    [...]

}
}

Building JFileChooser:


static File fileChooser(final int fileSelectionMode, final String dir) {

    JFileChooser jFileChooser = new JFileChooser();
    jFileChooser.setFileSelectionMode(fileSelectionMode);
    jFileChooser.setCurrentDirectory(new File(dir));
    Action details = jFileChooser.getActionMap().get("viewTypeDetails");
    details.actionPerformed(null);
    if (jFileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
        return jFileChooser.getSelectedFile();
    } else {
        return null;
    }
}

How to&Answers: