我试图从另一个类中禁用JComponent
,类似于模态对话。就我而言,我从Swing组件中调用JavaFX对话框;更具体地为FileChooser
。例如,因为showOpenDialog
需要javafx.stage.Window
作为参数,所以不能传递JComponent
。如何从另一个类中阻止JComponent?
我试过使用setEnabled(false)
和setEnabled(true)
,但是这有一个奇怪的副作用:一旦调用setEnabled(true)
,JFrame
将被最小化。调用setVisible(true)
可以解决这个问题,但会导致屏幕“闪烁”,因为帧会在短时间内消失。
只有当我使用CountDownLatch
来等待文件选择器的返回时才会出现此问题,这是必要的,否则它将立即返回,我将无法访问返回值。
这里是重现这一问题的SSCCE:
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame frame = new JFrame("Test");
JButton button = new JButton("Click me!");
JFXPanel jfxPanel = new JFXPanel();
FileChooser fileChooser = new FileChooser();
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setEnabled(false);
CountDownLatch latch = new CountDownLatch(1);
Platform.runLater(() -> {
fileChooser.showOpenDialog(null);
latch.countDown();
});
try {
latch.await();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
frame.setEnabled(true);
}
});
frame.add(button);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
});
}
是否有其他选项来阻止组件?
你想阻止'JFrame'完全(如无缩放),或者只是阻止与互动其内容? – 3ph3r
我认为后者就足够了。 – Veluria