views:

1118

answers:

6

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.

A: 

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
A: 

Probably add a FocusListener 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.

tulskiy
Can a modal dialog (and child components) loose focus for anything other than switching to another application?
vickirk
Ah, got what you mean, you meant instead of a modal dialog! Don't know why this was voted down
vickirk
+1  A: 

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.
Focus lost would have to ensure the component that gained the focus is not a child component of the dialog so you will need to search up the component hierarchy via `getParent()`
vickirk
@vickirk -- good point. I changed the example above to check for descendant components (arguably an edge case, but I enhanced the example to allow creating a child window of the dialog). I also changed it to use a window focus listener.
Chris B.
A: 

Use a WindowListener and handle the windowDeactivated() event.

camickr
A: 

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.

taftster
A: 

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