How to catch mouse click event on a window blocked by opened JDialog

1.8k Views Asked by At

To put it simple - what i want is to catch mouse click on a Window blocked by a modal JDialog.

Here is an example:

public class BlockedFrameTest
{
    public static void main ( final String[] args )
    {
        Toolkit.getDefaultToolkit ().addAWTEventListener ( new AWTEventListener ()
        {
            @Override
            public void eventDispatched ( final AWTEvent event )
            {
                if ( event instanceof MouseEvent )
                {
                    System.out.println ( event );
                }
            }
        }, AWTEvent.MOUSE_EVENT_MASK );

        final JFrame frame = new JFrame ( "Frame" );
        frame.add ( new JLabel ( "Content" )
        {
            {
                setBorder ( BorderFactory.createEmptyBorder ( 100, 100, 100, 100 ) );
            }
        } );
        frame.pack ();
        frame.setLocationRelativeTo ( null );
        frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
        frame.setVisible ( true );

        final JDialog dialog = new JDialog ( frame, "Dialog" );
        dialog.setModal ( true );
        dialog.add ( new JLabel ( "Content" )
        {
            {
                setBorder ( BorderFactory.createEmptyBorder ( 50, 50, 50, 50 ) );
            }
        } );
        dialog.pack ();
        dialog.setLocationRelativeTo ( frame );
        dialog.setVisible ( true );
    }
}

By looking at the example output log you will see that events from JFrame are not passed when JDialog is opened (even into the global AWT event listener added in the example).

So i wonder - is there any way to catch the click on the blocked JFrame?
Or at least catch an event when something blocked is "touched" by user?

The reason why i need this is to make custom-decorated JDialog blick when such event occurs.

2

There are 2 best solutions below

5
On

Maybe this helps :

how to obtain mouse click coordinates outside my window in Java

Its a bit difficult because you are leaving the realm of Swing and entering the native-GUI domain.

2
On

I think you can't do that if your JDialog is modal, but you can use next trick with FocusListener :

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Example {
    public static void main(final String[] args) {

        final JFrame frame = new JFrame("Frame");
        final JDialog dialog = new JDialog(frame, "Dialog");
        frame.add(new JLabel("Content") {
            {
                setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
            }
        });
        frame.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent arg0) {
                System.out.println("frame pressed");
                System.out.println("dialog focused " + dialog.isFocused());
                System.out.println("frame focused " + frame.isFocused());
                super.mousePressed(arg0);
            }
        });
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);


        dialog.add(new JLabel("Content") {
            {
                setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
            }
        });
        dialog.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent arg0) {
                super.focusLost(arg0);
                dialog.requestFocus();
            }
        });
        dialog.pack();
        dialog.setLocationRelativeTo(frame);
        dialog.setVisible(true);
    }
}

and output:

frame pressed
dialog focused true
frame focused false