java 当用户在 JDialog 之外单击时如何关闭模态 JDialog?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1675912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to close a modal JDialog when user clicks outside of JDialog?
提问by Pyrolistical
I have a Undecorated Modal JDialog which I want to setVisible(false) when the user clicks outside of the modal dialog.
我有一个未修饰的模态 JDialog,当用户在模态对话框外单击时,我想将其设置为 setVisible(false)。
Is this possible in Swing?
这在 Swing 中可能吗?
What I am doing is popping up a custom editor for a text field like a date selector. Is there an easier way to do what I want?
我正在做的是为文本字段(如日期选择器)弹出一个自定义编辑器。有没有更简单的方法来做我想做的事?
EDIT
编辑
Remember that modal blocks on the call to setVisible(true), so you can't just say "don't use a modal dialog"
请记住,调用 setVisible(true) 时模态会阻塞,因此您不能只说“不要使用模态对话框”
And I've tried focus listeners on the dialog, they don't trigger when its modal.
而且我已经尝试在对话框上使用焦点侦听器,它们不会在其模态时触发。
采纳答案by taftster
It's not a modal dialog if you can click outside of it and "something" happens. All the answers are correct, you should be creating a non-modal dialog and then deal with your use case via a FocusListener.
如果您可以在其外部单击并且“某些事情”发生,则它不是模态对话框。所有答案都是正确的,您应该创建一个非模态对话框,然后通过 FocusListener 处理您的用例。
回答by Chris B.
EDIT:Changed to use WindowFocusListener instead of FocusListener, as well as check for descending components on the focus lost in order to not hide if a child component gains focus.
编辑:更改为使用 WindowFocusListener 而不是 FocusListener,并检查丢失的焦点上的下降组件,以便在子组件获得焦点时不隐藏。
A simple way would be to add a window focus listener on the dialog that hides it when focus is lost. I don't see the need for modality in this case. For example:
一种简单的方法是在对话框上添加一个窗口焦点侦听器,在失去焦点时将其隐藏。在这种情况下,我认为不需要模态。例如:
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);
}
});
}
}
回答by Gill
It's not necessary to be a modal dialog (modal means that it prevents you from using the owner window until you hide the dialog). Better try this:
没有必要成为模态对话框(模态意味着它阻止您使用所有者窗口,直到您隐藏对话框)。最好试试这个:
final JDialog dlg ...
dlg.setModal(false);
dlg.addWindowFocusListener(new WindowFocusListener() {
public void windowLostFocus(WindowEvent e) {
dlg.setVisible(false);
}
public void windowGainedFocus(WindowEvent e) {
}
});
回答by Edgar Colmenares
Try to set the modal to false, and then use windowsDeactivated() for close de dialog (dialog.dispose()), works for me.
尝试将模态设置为 false,然后使用 windowsDeactivated() 关闭对话框(dialog.dispose()),对我有用。
回答by vickirk
Not really a modal dialog then if clicking else where closes it, maybe you want setAlwaysOnTop
不是真正的模态对话框,然后如果单击其他位置关闭它,也许您想要 setAlwaysOnTop
However, something like the following should do the trick (untested). Note, I would recommend moving the code into something better designed than use as provided.
但是,类似以下的内容应该可以解决问题(未经测试)。请注意,我建议将代码移动到比提供的使用更好的设计中。
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);
回答by Denis Tulskiy
Probably add a FocusListenerand hide the dialog when it looses the focus. May be tricky if some elements in the dialog can have focus. Anyways, experiment with it.
可能添加一个FocusListener并在失去焦点时隐藏对话框。如果对话框中的某些元素可以具有焦点,则可能会很棘手。无论如何,尝试一下。
回答by camickr
Use a WindowListener and handle the windowDeactivated() event.
使用 WindowListener 并处理 windowDeactivated() 事件。

