• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Mouse events not getting captured:

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First off, you guys are awesome. You have helped me before, and I appreciate it greatly.

This might actually be more appropriate for the threading and synchronization forum, but I thought I'd try this forum first, because I suspect my problem is in how I handle my canvas object:

I'm trying to make a fairly graphics intensive program (to be more blunt, a game) and I'm running into a problem with user input.

For some reason, my mouse events are not getting captured in my Swing JFrame. I'm trying to build a layer based application, where the mouse events captured by the frame get passed on up the line to objects that are supposed to handle them, but I think I'm lacking the conceptual understanding on how a mouseListener works, because it doesn't seem to be picked up by the frame itself.

Included is a SSCE. Run it, and click on the screen. It is intended that the console will print out at three lines per click, but it does not. In fact, in what is probably a related issue, the "X" button in the corner doesn't work either!

I've notice that when I comment out all the graphics code, my program (and SSCE) still fail to pick up mouse events. However, if I comment out all the graphics code as well as the thread.sleep call, then it works fine, up to and including the correct functionality of the "X" button. However, taking the thread.sleep out of the loop probably isn't a viable option, as my non-SSCE program is fairly graphics intensive and needs to be redrawn frequently, making a loop like this necessary.

In the SSCE, please forgive the abundance of code that doesn't really apply to the mouseListener problems. Lots of code is used to set the frame up, and another fair bit is used to set up the example "controller" layer that handles graphics and accepts mouseclicks. Please also forgive the cut-and-paste graphics code. I know how to handle a Graphics2D object once I have one, but figuring out how to get the right one to draw to is still somewhat of a dark art for me...

How can I make sure that my mouse events are getting captured, even while in a loop?

 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
comment out the
//while (true)

now see if the frame closes (might get you started in the right direction)

also, mouseClicked() is generally a poor choice - it only fires if the x,y
of mousePressed and mouseReleased is identical
 
Sheriff
Posts: 22817
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In other words, please read Concurrency in Swing.
 
B. Smith
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Props to Rob Spoor for giving me the correct reading material and letting me solve my own problem!

It turns out that I was running my graphics processing in the same thread that ran my event dispatching thread. In order to make my program work, I changed my main method to what follows:

That makes it work wonderfully. Thanks for hooking me up with that article.

If this approach raises warning bells to anybody, let me know. If nobody warns me away from this course of action, I'll mark this as resolved tomorrow.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is a SSCELayer? i cant find it in the jdk7 API.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randall Twede wrote:what is a SSCELayer? i cant find it in the jdk7 API.


That's a custom class.
Check line#80 in the code posted above
 
Rob Spoor
Sheriff
Posts: 22817
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

B. Smith wrote:That makes it work wonderfully.


But not correctly. You are still accessing a GUI component (the canvas, although indirectly) from a non-EDT thread. Any access to GUI components, both querying and modifying, should be done from the EDT.
Why don't you use a javax.swing.Timer instead? The ActionListener's actionPerformed method will be executed on the EDT, and it won't cause any threading issues.
 
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic