File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Detecting global mouse click Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Detecting global mouse click" Watch "Detecting global mouse click" New topic
Author

Detecting global mouse click

N D Fisher
Greenhorn

Joined: Jun 01, 2007
Posts: 27
Hi,

I have an application which has several transparent components, and I want to detect a mouse click on them. Unfortunately, transparent components don't seem to register any mouse events.

So is there any way of detecting a mouse click just on a kind of global basis? Once a click is detected, I can easily tell whether the mouse is inside my component by doing a "component.getBounds().contains(mousex, mousey)". All I really need is to detect the click, and which button it is that's been clicked.

Many thanks

Nick
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Perhaps you want to add a listener to the JFrame's glasspane?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38897
    
  23
For finding which button was clicked, it is far better to add Listeners to the individual buttons.
N D Fisher
Greenhorn

Joined: Jun 01, 2007
Posts: 27
Thanks for the replies.

Campbell - yes I already have a listener on the button but like I said, it does not fire an event when you click the mouse on it, because the button is transparent. The event only fires if I set the button to opaque, but that's not what I want.

Pete - thanks, I will give that a go and see if that works.


Nick
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10395
    
    8

Originally posted by N D Fisher:
.., because the button is transparent. The event only fires if I set the button to opaque..


This is strange. I have had transparent buttons in the past which responded to mouse clicks.
Are you calling setContentAreaFilled(false) to make it transparent? (As opposed to setOpaque(false))? Dont know if this matters, but it is recommended in the API


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
N D Fisher
Greenhorn

Joined: Jun 01, 2007
Posts: 27
Hi Maneesh,

Yes, I'm using setContentAreaFilled(false), not setOpaque(false).


Nick
N D Fisher
Greenhorn

Joined: Jun 01, 2007
Posts: 27
OK, just to let you know - I've tried adding a listener to the glass pane, but it still does not work.

If you set the glass pane to opaque, then yes it works. As soon as it's transparent, all events stop. I'm going round in circles.

Here is the problem in it's simplest form:



If you click on anything other than the border of the button, all events fail to work.


Nick
[ November 04, 2008: Message edited by: N D Fisher ]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10395
    
    8

Tried out your example.
1) Comment out myPanel.setOpaque(false);
2) Change myPanel.setBackground(new Color(30,190,250)); to myPanel.setBackground(new Color(30,190,250,1));
PS. Check out the API for the "alpha" argument.

Click on the button, observe the "you clicked my button"
Click on the panel, observe the "you clicked my panel"

Frankly speaking I do now know why the original code did not work. I personally haven't tried out the AWTUtilities class.

Anyone else can explain this?
N D Fisher
Greenhorn

Joined: Jun 01, 2007
Posts: 27
Thanks very much. Yes, I finally managed to work this out for myself last night (the exact same solution) - after many many weeks of tearing my hair out...

Why the transparency doesn't work on it's own, I have no idea. I'm not sure if this is as much a 'feature' of the API as it is a bug...

They certainly don't make it easy.

Nick
[ November 05, 2008: Message edited by: N D Fisher ]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10395
    
    8

One interesting point to note here is that AWTUtilities is packaged as com.sun.awt.AWTUtilities.
If I understand this correctly, (the com.sun..packaging), then
While this class is public, it should not be treated as
public API and its API may change in incompatable ways between dot dot
releases and even patch releases. You should not rely on this class even
existing.
Sergiy Goncharenko
Greenhorn

Joined: Jun 08, 2012
Posts: 2
N D Fisher wrote:Hi,

I have an application which has several transparent components, and I want to detect a mouse click on them. Unfortunately, transparent components don't seem to register any mouse events.

So is there any way of detecting a mouse click just on a kind of global basis? Once a click is detected, I can easily tell whether the mouse is inside my component by doing a "component.getBounds().contains(mousex, mousey)". All I really need is to detect the click, and which button it is that's been clicked.

Many thanks

Nick


Display.getDefault().addFilter(SWT.MouseDown, listener);
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
why post a (presumably) SWT solution (SWT.MouseDown) to a 4-year-old Swing problem?
Sergiy Goncharenko
Greenhorn

Joined: Jun 08, 2012
Posts: 2
Michael Dunn wrote:why post a (presumably) SWT solution (SWT.MouseDown) to a 4-year-old Swing problem?


Because, caption of this topic is "Detecting global mouse click" and forum section is "Swing / AWT / SWT / JFace".
People try to find how to listen global mouse click on java UI and not found solution for SWT. I myself was a hostage of this situation.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

We like having answers posted, because that's one of the reasons that the forum exists. It's true that the answer which Sergiy posted isn't an answer to the original question, but... like he says... it's an answer to a similar question which he had. And it's true, somebody searching for an answer to that question might be sent here by an internet search.

And since the original poster is long gone, you could hardly consider his post to be hijacking the thread. So I wouldn't consider it worth complaining about, really.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38897
    
  23
And welcome to the Ranch Sergiy Goncharenko
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Detecting global mouse click