Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to grab pointer

28 views
Skip to first unread message

FredK

unread,
Aug 15, 2012, 1:45:25 PM8/15/12
to
How do I grab the pointer (similar to X's XGrabPointer), such that all mouse events will be sent only to my component that has the grab, until I relaease the grab?

I want to initiate the grab, then allow the user to go anywhere on the screen and click somewhere; I will capture the location of the click and do something appropriate, depending on where the click occurred.

Knute Johnson

unread,
Aug 15, 2012, 3:09:15 PM8/15/12
to
On your Java program or on the desktop?

--

Knute Johnson

FredK

unread,
Aug 15, 2012, 3:45:36 PM8/15/12
to
On Wednesday, August 15, 2012 12:09:15 PM UTC-7, Knute Johnson wrote:
> On 8/15/2012 10:45 AM, FredK wrote: > How do I grab the pointer (similar to X's XGrabPointer), such that > all mouse events will be sent only to my component that has the grab, > until I relaease the grab? > > I want to initiate the grab, then allow the user to go anywhere on > the screen and click somewhere; I will capture the location of the > click and do something appropriate, depending on where the click > occurred. > On your Java program or on the desktop? -- Knute Johnson

I would like to be able to do both. The user clicks on a "Select" button, then I want to let her move the mouse until it is over some component, then click there to identify the component of interest. I do not want that component to get a mouse event; I only want to know the coordinates of the click.

markspace

unread,
Aug 15, 2012, 4:28:00 PM8/15/12
to
On 8/15/2012 12:45 PM, FredK wrote:

> I would like to be able to do both. The user clicks on a "Select"
> button, then I want to let her move the mouse until it is over some
> component, then click there to identify the component of interest. I
> do not want that component to get a mouse event; I only want to know
> the coordinates of the click.
>

What the goal of this request? If you're looking to copy mouse clicks,
Java has a "Robot" that records mouse clicks and can replay them. You
might also look at the Robot code to see how they implement it.

<http://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html>

FredK

unread,
Aug 15, 2012, 4:40:30 PM8/15/12
to
On Wednesday, August 15, 2012 1:28:00 PM UTC-7, markspace wrote:
> On 8/15/2012 12:45 PM, FredK wrote: > I would like to be able to do both. The user clicks on a "Select" > button, then I want to let her move the mouse until it is over some > component, then click there to identify the component of interest. I > do not want that component to get a mouse event; I only want to know > the coordinates of the click. > What the goal of this request? If you're looking to copy mouse clicks, Java has a "Robot" that records mouse clicks and can replay them. You might also look at the Robot code to see how they implement it. <http://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html>


An app similar to EditRes, but for Java apps instead of X/Motif.

I generate a tree showing all of the GUI components of the app. I want the user to be able to point to a component in the app, then that item will be displayed as selected in the tree. So when the user presses the "Select" button, the cursor changes (say, to "hand"), my comnponent grabs the mouse so only it gets events; then thye user moves the cursor until is over some component anywhere on the screen, and clicks there. My component receives the press event and figures out what component it is over and highlights the tree node that represents that component. I do not want the press event to be sent to the component under the click.

markspace

unread,
Aug 15, 2012, 5:30:57 PM8/15/12
to
On 8/15/2012 1:40 PM, FredK wrote:
>
> An app similar to EditRes, but for Java apps instead of X/Motif.


I have no idea what EditRes is or doesn. ;-)


> I generate a tree showing all of the GUI components of the app. I
> want the user to be able to point to a component in the app, then
> that item will be displayed as selected in the tree. So when the user
> presses the "Select" button, the cursor changes (say, to "hand"), my
> comnponent grabs the mouse so only it gets events; then thye user


Take a look at The Glass Pane parts of this tutorial:

<http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html>

"The glass pane is useful when you want to be able to catch events or
paint over an area that already contains one or more components. For
example, you can deactivate mouse events for a multi-component region by
having the glass pane intercept the events."


Jeff Higgins

unread,
Aug 15, 2012, 5:48:21 PM8/15/12
to
On 08/15/2012 04:40 PM, FredK wrote:
>
> I generate a tree showing all of the GUI components of the app. I
> want the user to be able to point to a component in the app, then
> that item will be displayed as selected in the tree. So when the user
> presses the "Select" button, the cursor changes (say, to "hand"), my
> comnponent grabs the mouse so only it gets events; then thye user
> moves the cursor until is over some component anywhere on the screen,
> and clicks there.

Each top level window that your application creates will
generate mousein/mouseout, focusin/focusout events.
Whats the problem?

Knute Johnson

unread,
Aug 15, 2012, 6:05:07 PM8/15/12
to
On 8/15/2012 1:40 PM, FredK wrote:
It is still not that clear what you want to do but you can write a
listener and then register it with all of the components in your
application. You could then determine the component that the mouse was
over and its position. You can even determine the absolute position,
screen coordinates, of the mouse, when the event happened, the name of
the component, etc.

--

Knute Johnson

Jeff Higgins

unread,
Aug 15, 2012, 6:18:20 PM8/15/12
to
On 08/15/2012 05:48 PM, Jeff Higgins wrote:
> On 08/15/2012 04:40 PM, FredK wrote:
>>
>> I generate a tree showing all of the GUI components of the app. I
>> want the user to be able to point to a component in the app, then
>> that item will be displayed as selected in the tree. So when the user
>> presses the "Select" button, the cursor changes (say, to "hand"), my
>> comnponent grabs the mouse so only it gets events; then thye user
>> moves the cursor until is over some component anywhere on the screen,
>> and clicks there.
>
> Each top level window that your application creates will
> generate mousein/mouseout, focusin/focusout events.
> Whats the problem?
>

You could even (and I just learned this, thanks)
Toolkit.getDefaultToolkit().addAWTEventListener(l);
plus
see Window.getWindows().

Jeff Higgins

unread,
Aug 15, 2012, 7:48:57 PM8/15/12
to
On 08/15/2012 06:18 PM, Jeff Higgins wrote:
> On 08/15/2012 05:48 PM, Jeff Higgins wrote:
>> On 08/15/2012 04:40 PM, FredK wrote:
>>>
>>> I generate a tree showing all of the GUI components of the app. I
>>> want the user to be able to point to a component in the app, then
>>> that item will be displayed as selected in the tree. So when the user
>>> presses the "Select" button, the cursor changes (say, to "hand"), my
>>> comnponent grabs the mouse so only it gets events;

There's really no such thing as "grabbing the mouse". Events are fired
(put on the event queue) and consumed (how you do that is up to you).

If your application has a mode of operation called
componentSelectorToolActive and you want all of certain events to be
processed by your ComponentSelectorTool then you will need to arrange
that.

Jeff Higgins

unread,
Aug 15, 2012, 8:03:03 PM8/15/12
to
You should probably be thinking in terms of "when my
ComponentSelectorTool receives the press event it notifies my
ComponentTreeDisplayWidget ...".

Jeff Higgins

unread,
Aug 15, 2012, 8:16:52 PM8/15/12
to
Just remember that events are placed on the queue in the order that
they are received but that event listeners can be notified in any order.

Hope it helps. over

Jeff Higgins

unread,
Aug 15, 2012, 8:58:45 PM8/15/12
to
On 08/15/2012 08:16 PM, Jeff Higgins wrote:
> On 08/15/2012 08:03 PM, Jeff Higgins wrote:
>> On 08/15/2012 07:48 PM, Jeff Higgins wrote:
>>> On 08/15/2012 06:18 PM, Jeff Higgins wrote:
>>>> On 08/15/2012 05:48 PM, Jeff Higgins wrote:
>>>>> On 08/15/2012 04:40 PM, FredK wrote:
>>>>>>
>>>>>> I generate a tree showing all of the GUI components of the app. I
>>>>>> want the user to be able to point to a component in the app, then
>>>>>> that item will be displayed as selected in the tree. So when the user
>>>>>> presses the "Select" button, the cursor changes (say, to "hand"), my
>>>>>> comnponent grabs the mouse so only it gets events;
>>>
>>> There's really no such thing as "grabbing the mouse". Events are fired
>>> (put on the event queue) and consumed (how you do that is up to you).
>>>
>>> If your application has a mode of operation called
>>> componentSelectorToolActive and you want all of certain events to be
>>> processed by your ComponentSelectorTool then you will need to arrange
>>> that.
>>>
>>> then thye user
>>>>>> moves the cursor until is over some component anywhere on the screen,
>>>>>> and clicks there.

If this "component" is within your application containment hierarchies
fine, if you are thinking of some random visual item on your platform
desktop not associated with the AWT containment hierarchy you're out of
luck with the Java APIs.

Jeff Higgins

unread,
Aug 15, 2012, 9:34:06 PM8/15/12
to
Also keep in mind that there is no central AWT Window Server
that keeps track of and communicates with AWT clients like
in the X windowing system.

FredK

unread,
Aug 16, 2012, 2:03:51 PM8/16/12
to
On Wednesday, August 15, 2012 6:34:06 PM UTC-7, Jeff Higgins wrote:
> On 08/15/2012 08:58 PM, Jeff Higgins wrote: > On 08/15/2012 08:16 PM, Jeff Higgins wrote: >> On 08/15/2012 08:03 PM, Jeff Higgins wrote: >>> On 08/15/2012 07:48 PM, Jeff Higgins wrote: >>>> On 08/15/2012 06:18 PM, Jeff Higgins wrote: >>>>> On 08/15/2012 05:48 PM, Jeff Higgins wrote: >>>>>> On 08/15/2012 04:40 PM, FredK wrote: >>>>>>> >>>>>>> I generate a tree showing all of the GUI components of the app. I >>>>>>> want the user to be able to point to a component in the app, then >>>>>>> that item will be displayed as selected in the tree. So when the >>>>>>> user >>>>>>> presses the "Select" button, the cursor changes (say, to "hand"), my >>>>>>> comnponent grabs the mouse so only it gets events; >>>> >>>> There's really no such thing as "grabbing the mouse". Events are fired >>>> (put on the event queue) and consumed (how you do that is up to you). >>>> >>>> If your application has a mode of operation called >>>> componentSelectorToolActive and you want all of certain events to be >>>> processed by your ComponentSelectorTool then you will need to arrange >>>> that. >>>> >>>> then thye user >>>>>>> moves the cursor until is over some component anywhere on the >>>>>>> screen, >>>>>>> and clicks there. > > If this "component" is within your application containment hierarchies > fine, if you are thinking of some random visual item on your platform > desktop not associated with the AWT containment hierarchy you're out of > luck with the Java APIs. Also keep in mind that there is no central AWT Window Server that keeps track of and communicates with AWT clients like in the X windowing system. > >>>>>> >>>>>> Each top level window that your application creates will >>>>>> generate mousein/mouseout, focusin/focusout events. >>>>>> Whats the problem? >>>>>> >>>>> >>>>> You could even (and I just learned this, thanks) >>>>> Toolkit.getDefaultToolkit().addAWTEventListener(l); >>>>> plus >>>>> see Window.getWindows(). >>>>> >>>>>> My component receives the press event and figures >>>>>>> out what component it is over and highlights the tree node that >>>>>>> represents that component. I do not want the press event to be sent >>>>>>> to the component under the click. >>> You should probably be thinking in terms of "when my >>> ComponentSelectorTool receives the press event it notifies my >>> ComponentTreeDisplayWidget ...". >>> >> Just remember that events are placed on the queue in the order that >> they are received but that event listeners can be notified in any order. >> >> Hope it helps. over >> >


Using the glassPane solves most of what I wanted to do.

Jeff Higgins

unread,
Aug 16, 2012, 4:54:18 PM8/16/12
to
On 08/16/2012 02:03 PM, FredK wrote:
>
> Using the glassPane solves most of what I wanted to do.

Great.
As I read your description I kept wondering if you were talking
about Components in your singular application or you were asking
about crossing application (JVM) barriers.
This led me to wonder about discovering and communication with
disparate local JVM instances. It turns out there is an Attach API
just for that. Thanks for helping widen my horizons. :)


FredK

unread,
Aug 16, 2012, 6:10:42 PM8/16/12
to
On Thursday, August 16, 2012 1:54:18 PM UTC-7, Jeff Higgins wrote:
> On 08/16/2012 02:03 PM, FredK wrote: > > Using the glassPane solves most of what I wanted to do. Great. As I read your description I kept wondering if you were talking about Components in your singular application or you were asking about crossing application (JVM) barriers. This led me to wonder about discovering and communication with disparate local JVM instances. It turns out there is an Attach API just for that. Thanks for helping widen my horizons. :)

Actually, that is the part that I was referring to when I said "most of what I want to do". I would like to be able to interrogate other apps, too.

This tool ("GuiViewer") will display a tree of the JComponent hierarchy of a JFrame (or JDialog), and then if you click on a node in the tree, it will temporarily place a flashing panel over the component (set to the component's size and location) that the node represents. It's a great way to check what's wrong when your GUI doesn't come out the way you intended.

Alternately, when I show the glasspane over the app, you can click over any spot in the app and the GuiViewer will select the node that represents the component under the mouse, and will flash a panel over it.

It would be nice to be able to do that from a separate GuiViewer, without the app having to create an instance of my GuiViewer.

John B. Matthews

unread,
Aug 16, 2012, 8:40:53 PM8/16/12
to
In article <k0jmdc$v85$1...@dont-email.me>,
It looks like <http://visualvm.java.net/>, included in the JDK, uses
this API: <http://visualvm.java.net/nonav/apidocs/13/index.html>.

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Jeff Higgins

unread,
Aug 16, 2012, 9:11:42 PM8/16/12
to
On 08/16/2012 08:40 PM, John B. Matthews wrote:
> In article<k0jmdc$v85$1...@dont-email.me>,
> Jeff Higgins<je...@invalid.invalid> wrote:
>
>> On 08/16/2012 02:03 PM, FredK wrote:
>>>
>>> Using the glassPane solves most of what I wanted to do.
>>
>> Great.
>> As I read your description I kept wondering if you were talking about
>> Components in your singular application or you were asking about
>> crossing application (JVM) barriers. This led me to wonder about
>> discovering and communication with disparate local JVM instances. It
>> turns out there is an Attach API just for that. Thanks for helping
>> widen my horizons. :)
>
> It looks like<http://visualvm.java.net/>, included in the JDK, uses
> this API:<http://visualvm.java.net/nonav/apidocs/13/index.html>.
>
Great!
com.sun.tools.visualvm.application.jvm.Jvm
Now I've even more to do. Thanks.

Jeff Higgins

unread,
Aug 16, 2012, 9:17:07 PM8/16/12
to
Ah. Please keep us informed of your progress.

Jeff Higgins

unread,
Aug 22, 2012, 7:54:59 PM8/22/12
to
On 08/15/2012 04:40 PM, FredK wrote:
> On Wednesday, August 15, 2012 1:28:00 PM UTC-7, markspace wrote:
>> On 8/15/2012 12:45 PM, FredK wrote:> I would like to be able to do both. The user clicks on a "Select"> button, then I want to let her move the mouse until it is over some> component, then click there to identify the component of interest. I> do not want that component to get a mouse event; I only want to know> the coordinates of the click.> What the goal of this request? If you're looking to copy mouse clicks, Java has a "Robot" that records mouse clicks and can replay them. You might also look at the Robot code to see how they implement it.<http://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html>
>
>
> An app similar to EditRes, but for Java apps instead of X/Motif.

Not cross application but may be interesting.
<http://fxexperience.com/scenic-view/>

Jeff Higgins

unread,
Aug 22, 2012, 8:45:04 PM8/22/12
to
On 08/22/2012 07:54 PM, Jeff Higgins wrote:
> On 08/15/2012 04:40 PM, FredK wrote:
>>
>> An app similar to EditRes, but for Java apps instead of X/Motif.
>
> Not cross application
I'm not sure what prompted that comment.

Still
> but may be interesting.

> <http://fxexperience.com/scenic-view/>
>
JavaFX not Swing, but still...


0 new messages