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.

asked Dec 20, 2013 at 11:40
0

2 Answers 2

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.

answered Dec 20, 2013 at 11:49
Sign up to request clarification or add additional context in comments.

5 Comments

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.
hmm difficult. As soon as you are clicking outside of an application then technically you are not in Java world anymore.
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.
what if the user clicks outside of the underlying frame as well? Would they just be ignored?
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.
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
answered Dec 20, 2013 at 11:53

2 Comments

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.
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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.