Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes how do i place a mouse over action listener on a rectangle? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how do i place a mouse over action listener on a rectangle? " Watch "how do i place a mouse over action listener on a rectangle? " New topic
Author

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

jite eghagha
Ranch Hand

Joined: Oct 06, 2006
Posts: 127
hi,

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


Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
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.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Also, you most definitely do not need nor want to add any kind of listener in a painting method override.


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2155
    
    7
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how do i place a mouse over action listener on a rectangle?
 
Similar Threads
Tooltip on Line2D or Rectangle2D
Adding an image to a JTree node
My panel object won't repaint(). Please help.
mouseOver
textfield and listener