[英]JFrame is disabled after closing JFileChooser and JDialog
我真的需要你的幫助,因為我正在為此苦苦掙扎。 我已經創建了一個 JFrame,我可以在其中打開一個 JDialog,例如更改設置。 在 JDialog 中有一個用於啟動 JFileChooser 的按鈕。 我可以選擇一個文件,一切正常。 但是如果我只是關閉 JFileChooser 和 JDialog,JFrame 將禁用並最小化。
有誰知道如何解決這個問題?
構建 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);
構建 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());
}
});
[...]
pack();
setVisible(true);
setModal(true);
}
}
構建 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;
}
}
找到了解決辦法:
在 EditColumnsDialog (JDialog) 中設置 "setModal(true)" BEFORE (!!!) "setVisible(true)"
這將確保在 JFrame 中打開的新窗口 (JDialog) 阻塞舊窗口,然后 JFileChooser 窗口阻塞 JDialog 並且在關閉它后另一個將獲得焦點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.