我有一个未装饰的模态 JDialog,当用户在模态对话框之外单击时,我想将其设置为 setVisible(false)。
这在 Swing 中可能吗?
我正在做的是为日期选择器之类的文本字段弹出一个自定义编辑器。有没有更简单的方法来做我想做的事?
编辑
请记住,调用 setVisible(true) 时模态会阻塞,因此您不能只说“不要使用模态对话框”
而且我已经尝试将焦点监听器放在对话框上,它们不会在其模态时触发。
我有一个未装饰的模态 JDialog,当用户在模态对话框之外单击时,我想将其设置为 setVisible(false)。
这在 Swing 中可能吗?
我正在做的是为日期选择器之类的文本字段弹出一个自定义编辑器。有没有更简单的方法来做我想做的事?
编辑
请记住,调用 setVisible(true) 时模态会阻塞,因此您不能只说“不要使用模态对话框”
而且我已经尝试将焦点监听器放在对话框上,它们不会在其模态时触发。
编辑: 更改为使用 WindowFocusListener 而不是 FocusListener,以及检查焦点丢失的降序组件,以便在子组件获得焦点时不隐藏。
一种简单的方法是在对话框上添加一个窗口焦点侦听器,当焦点丢失时将其隐藏。在这种情况下,我认为不需要模态。例如:
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class ClickAwayDialog extends JDialog {
public ClickAwayDialog(final Frame owner) {
super(owner);
JPanel pnl = new JPanel(new BorderLayout());
pnl.add(new JLabel("Click outside this dialog in the parent frame to close it"), BorderLayout.NORTH);
JButton btn = new JButton("Click Me");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(ClickAwayDialog.this, "New Child Window");
}
});
pnl.add(btn, BorderLayout.CENTER);
this.setContentPane(pnl);
this.pack();
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setLocationRelativeTo(owner);
this.setAlwaysOnTop(true);
this.addWindowFocusListener(new WindowFocusListener() {
public void windowGainedFocus(WindowEvent e) {
//do nothing
}
public void windowLostFocus(WindowEvent e) {
if (SwingUtilities.isDescendingFrom(e.getOppositeWindow(), ClickAwayDialog.this)) {
return;
}
ClickAwayDialog.this.setVisible(false);
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame parent = new JFrame();
parent.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
parent.setSize(300, 300);
parent.setLocationByPlatform(true);
parent.setVisible(true);
ClickAwayDialog dlg = new ClickAwayDialog(parent);
dlg.setVisible(true);
}
});
}
}
如果您可以在其外部单击并且“某事”发生,则它不是模态对话框。所有答案都是正确的,您应该创建一个非模态对话框,然后通过 FocusListener 处理您的用例。
不必是模态对话框(模态意味着它会阻止您使用所有者窗口,直到您隐藏对话框)。最好试试这个:
final JDialog dlg ...
dlg.setModal(false);
dlg.addWindowFocusListener(new WindowFocusListener() {
public void windowLostFocus(WindowEvent e) {
dlg.setVisible(false);
}
public void windowGainedFocus(WindowEvent e) {
}
});
尝试将模式设置为 false,然后使用 windowsDeactivated() 关闭对话框(dialog.dispose()),对我有用。
使用 WindowListener 并处理 windowDeactivated() 事件。
不是真正的模态对话框,然后如果单击其他位置关闭它,也许你想要setAlwaysOnTop
但是,像下面这样的东西应该可以解决问题(未经测试)。请注意,我建议将代码移动到比提供的使用更好的设计中。
static JDialog dialog = ...
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
dialog.setVisible(false);
SwingUtils.invokeLater(new Runnable(){
public void run(){
Toolkit.getDefaultToolkit().removeAWTEventListener(this);
}
});
}
}, AWTEvent.MOUSE_EVENT_MASK);
dialog.setVisible(true);
可能会添加一个FocusListener并在失去焦点时隐藏对话框。如果对话框中的某些元素可以具有焦点,则可能会很棘手。无论如何,尝试一下。