0

I have a JFrame, which opens a modal JDialog. The JFrame has quiet a few buttons, wich are NOT supposed to be usable, when the JDialog is open -> therefore, modal JDialog. But when the area outside the JDialog is clicked, the JDialog is supposed to close. The solution I outline below doesn't catch any MouseEvents on the JFrame, though.

I now need to figure out if a user clicked on the Window but outside the JDialog.
Reason to do this as per comment: The JDialog is supposed to close once the user clicks outside it!
My best guess currently is to create an AWTEventListener (As in how to obtain mouse click coordinates outside my window in Java):

        listener = new AWTEventListener() {
        @Override
        public void eventDispatched(AWTEvent event) {
            if (!event.getSource().getClass().equals(ButtonDialog.class)
                    && event.getID() == 502) {
                ButtonDialog.this.dispose();
            }
        }
    };

and then add it when the JDialog opened:

                Toolkit.getDefaultToolkit().addAWTEventListener(
                    listener, AWTEvent.MOUSE_EVENT_MASK);

Unfortunately, when the Dialog is modal, no AWT-events seem to be happening outside that Dialog.
I have considered using the GlassPane of the JFrame instead, but that really seems impractical.
Can I catch events on a "modally-blocked" Windows like this at all? Or am I on the totally wrong track?

Community
  • 1
  • 1
Layna
  • 457
  • 5
  • 19
  • 2
    *"I now need to figure out if a user clicked on the Window but outside the JDialog."* ..why? See [What is the XY problem?](http://meta.stackexchange.com/q/66377) – Andrew Thompson Nov 26 '15 at 11:42
  • Thanks! Yep, I actually totally forgot why I even need to catch that click ^^ – Layna Nov 26 '15 at 11:45
  • Voting to close as a problem that can no longer be reproduced; if not, please update the question. – trashgod Nov 26 '15 at 12:35
  • Please explain the close request? I still have the problem... or is this a request for more example code? – Layna Nov 26 '15 at 12:39
  • I interpreted your comment to mean that you no longer "need to catch that click." If you _do_ need to respond to a click, why not use a modeless dialog? – trashgod Nov 26 '15 at 13:27
  • The widow that opened the JDialog is supposed to be blocked -> modal does that just fine. But IF someone clicks outside the Modal Dialog, it is supposed to close. – Layna Nov 26 '15 at 13:31
  • 1
    I don't this your users understand the concept of a blocking dialog. Why make it blocking if you can just click on the window to close the dialog? You are not describing your problem effectively. I also agree this is an XY problem. – camickr Nov 26 '15 at 16:10

0 Answers0