Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Inject fake events into an AWT container.  RSS feed

 
Theodore Norvell
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose I have a container c and an (x,y) point relative to that container. I'd like to send a mouse event (say mouse pressed) to the right component within c. Which is the right component? It is the one that would get the event if the mouse had actually at location (x,y) and pressed.

My outline is


Is there an easy way to find the right subcomponent?

So far I've constructed a recursive search based on the nonpublic method Container.getMouseEventTarget.
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
look at the Robot class, I have successfully used it to mimic mouse interactions in the past.
 
Theodore Norvell
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I should have mentioned that I'd like to be able to do this in a headless environment. That rules out Robot, I think.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Theodore Norvell wrote:Thanks. I should have mentioned that I'd like to be able to do this in a headless environment. That rules out Robot, I think.

Umm wouldn't that rule out mouse (and effectively) mouse events as well?

Headless mode is a system configuration in which the display device, keyboard, or mouse is lacking. Sounds unexpected, but actually you can perform different operations in this mode, even with graphic data.

http://www.oracle.com/technetwork/articles/javase/headless-136834.html
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there an easy way to find the right subcomponent?


 
Theodore Norvell
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mouse events work just fine in a headless environment.
 
Theodore Norvell
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Container.getComponentAt is not sufficient for two reasons. First the actual component that should be the source may be many layers deep. I could use findCompoentAt instead to solve that. The second problem is that the component returned by getComponentAt or findComponentAt may not be prepared to handle the event. Consider this hierarchy

JPanel A contains JPanel B contains JButton C contains JLabel D

where only C is prepared to handle mouse events. (x,y) is contained in D. getComponentAt gives B while findComponentAt returns D. The one I want is C.
 
Stephan van Hulst
Saloon Keeper
Posts: 7806
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AWT is more than capable of dispatching the event to the correct component. Use getComponent() in combination with dispatchEvent().
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First the actual component that should be the source may be many layers deep. I could use findCompoentAt instead to solve that.


So why didn't you mention that in your question, so people don't make the same suggestion?

The second problem is that the component returned by getComponentAt or findComponentAt may not be prepared to handle the event


Again, why was the more specific requirement not mentioned.

Crossposted: http://stackoverflow.com/questions/33376305/given-a-swing-container-and-coordinates-how-can-i-find-the-source-for-a-manufac

Why was that posting not updated with the information you have found to benefit anybody (like me) who might read the question.

A forum is about everybody contributing.

Again, in your last question you were asked to post a SSCCE to demonstrate your problem. Don't make us guess what all the potential problems might be. Our time is better spend not guessing.
 
Theodore Norvell
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob: Thanks for mentioning the crossposting on stack overflow. The specific requirements were mentioned in my first post: I need the component that would be the source for a mouse event. There are a lot of ways that don't work, I'm looking for one that does.

Below is my current approach. (Developed after I posted the original question.) I'd like to find one that is simpler and that deals correctly with the event mask. For anyone who is tempted to use the following code, I'd caution that it is not thoroughly tested.


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!