1

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.

Mikle Garin
  • 10,083
  • 37
  • 59

2 Answers2

1

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.

Community
  • 1
  • 1
Oliver Watkins
  • 12,575
  • 33
  • 119
  • 225
  • I know that using native feature to catch such event might be a solution, but i won't use it as it will only add a lot of mess in the code and will make it unmaintainable some day - I would rather keep it pure-java. – Mikle Garin Dec 20 '13 at 11:54
  • hmm difficult. As soon as you are clicking outside of an application then technically you are not in Java world anymore. – Oliver Watkins Dec 20 '13 at 11:56
  • Not really, I am just clicking another part of the same Java application - i mentioned that i only need to catch events when user clicks some other part of the same application that is blocked by the dialog, not another random application launched in the system. – Mikle Garin Dec 20 '13 at 11:59
  • what if the user clicks outside of the underlying frame as well? Would they just be ignored? – Oliver Watkins Dec 20 '13 at 12:01
  • Yes, i don't really care about clicks outside of the application as they don't even have to produce "blinking" effect on the dialog. Just see how Windows (i mean OS) decoration acts when you click on the frame in my example. – Mikle Garin Dec 20 '13 at 12:03
1

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
alex2410
  • 10,904
  • 3
  • 25
  • 41
  • That would make interactions with frame content possible, which is a bad thing. And it won't fully mimic modality, so i'd rather avoid using such tricks. – Mikle Garin Dec 20 '13 at 12:00
  • I know, but i rather use modal JDialog than some workaround as it is more convenient, will (for sure) act properly on different platforms and won't require any code to support. – Mikle Garin Dec 20 '13 at 12:12