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?