• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

how do i place a mouse over action listener on a rectangle?

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

how do i place a mouse over action listener on a rectangle?


 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Listeners apply to components or containers, not to areas within them. You'll need to store the coordinates of the rectangle you're interested in, and -in the listener- check whether the X/Y values of the event are within the rectangle. The Rectangle.contains method is useful for that.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you most definitely do not need nor want to add any kind of listener in a painting method override.
 
Sheriff
Posts: 21760
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need a MouseMotionListener and listen to the mouseMoved event. Take care though, because there will be many of these events if you move your mouse across the component.

And speaking of the component, are you sure you want to override paint? With JComponent and its (direct and indirect) subclasses, it's recommended to override paintComponent. And the first call should be super.paintComponent(g);. You're missing the equivalent call in your method; if you still will be overriding paint, at least call super.paint(g) at the start of the method.
 
Rancher
Posts: 3053
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you just have a Rectangle, then create a JPanel and add a LineBorder to it. Then add your "rectangle panel" to your GUI. Now you can add a MouseListener to the panel component and listen for MouseEvents.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!