How to Close a Modal?

How to close a modal JDialog when user clicks outside of JDialog?

  • I have a Undecorated Modal JDialog which I want to setVisible(false) when the user clicks outside of the modal dialog. Is this possible in 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" And I've tried focus listeners on the dialog, they don't trigger when its modal.

  • Answer:

    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.

Pyrolistical at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

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. 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); } }); } }

Chris B.

Not really a modal dialog then if clicking else where closes it, maybe you want 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);

vickirk

Probably add a http://java.sun.com/docs/books/tutorial/uiswing/events/focuslistener.html and hide the dialog when it looses the focus. May be tricky if some elements in the dialog can have focus. Anyways, experiment with it.

Denis Tulskiy

Use a WindowListener and handle the windowDeactivated() event.

camickr

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) { } });

Gill

Try to set the modal to false, and then use windowsDeactivated() for close de dialog (dialog.dispose()), works for me.

Edgar Colmenares

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.