aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Creating mouse rollover for for custom drawn JButton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Creating mouse rollover for for custom drawn JButton" Watch "Creating mouse rollover for for custom drawn JButton" New topic
Author

Creating mouse rollover for for custom drawn JButton

Chris Baty
Ranch Hand

Joined: Nov 23, 2008
Posts: 30
Hi guys,
Does anyone know how to create a rollover for a custom drawn button? It's easy if you use Icon but mine is custom drawn. If I implement the mouselistener in the myButton class where do I add the listener. If I implement the listener in the JFrame class how does that class know only to redraw that button? I'm confused.

Any help would be appreciated.

Chris
Chris Nash
Greenhorn

Joined: May 20, 2009
Posts: 28
Make the listener an inner class of myButton. Use a MouseAdapter and extend it. Add the MouseListner in the myButton constructor. Then all you have to do is implement your logic in the mouseEntered() and mouseExited() events in the adapter. Ask if you need any more direction. HTH


- Frecklefoot
"What you are good at is not as important as being good at something." --Robert B.Parker
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Chris Baty wrote:Hi guys,
Does anyone know how to create a rollover for a custom drawn button? It's easy if you use Icon but mine is custom drawn. If I implement the mouselistener in the myButton class where do I add the listener. If I implement the listener in the JFrame class how does that class know only to redraw that button? I'm confused.

Any help would be appreciated.

Chris


What do you mean by a "custom drawn button"? Do you mean that you're extending JButton and painting in its paintComponent method, look and feel be darned? If so then just call isRollover() in an if block in your paintComponent method.

Otherwise, you can add a change listener to the button's model and check to see if the rollover field has changed, again with the isRollover() method.

For an example of both:
Chris Baty
Ranch Hand

Joined: Nov 23, 2008
Posts: 30
Thanks. I'll give it a go.
Chris
Chris Baty
Ranch Hand

Joined: Nov 23, 2008
Posts: 30
Okay, I'm still confused. I added a MouseListener subclass to my JButton class:


How does the MouseListener subclass tell paintComponent() to run again?

Then I did this in the JButton constructor:


How do I add the MouseListener to my test class? I tried:


Thanks again.
chris
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
You've got your listener ideas all wrong. You would benefit by reading (re-reading?) the listener section of the tutorials. But also again, as per my recommendations though, I wouldn't use a mouse listener.
Chris Baty
Ranch Hand

Joined: Nov 23, 2008
Posts: 30
Okay, this is my second attempt. Here's what I got in my test program:


Here's my button class:


It still doesn't respond. The other issue I have is eventually these buttons will be keys in a virtual keyboard. If that's the case should I use a MouseListener anyway since I'll need to return the button label?
Thanks.
Chris
Chris Nash
Greenhorn

Joined: May 20, 2009
Posts: 28
Chris, Pete and I were telling you to do two different things. His suggestion is to extend JButton and implement your own paint routine. He says there is an isRollover() method you can call to check for rollover. In his case, you won't need a mouse listener.

My suggestion was to ditch JButton altogether and instead extend JLabel and, in effect, make your own button with a mouse listener. My suggestion is more work, but it worked for me. His is probably simpler and leverages the existing JButton API. It's your choice. We're talking about two different approaches. Don't mix and match them. If you want to do what he suggested, ignore everything I suggested, and vice-versa if you want to take my route. My recommendation is to follow his advice since he provided code samples and probably has more experience with this type of stuff.
Chris Baty
Ranch Hand

Joined: Nov 23, 2008
Posts: 30
Okay, I'm an idiot sometimes. This is what I got:


I know the mouselistener works because when I click on the button I get letter "A". But I still don't understand how to tell the button to repaint itself.

Thanks for all the help so far.
Chris
Chris Baty
Ranch Hand

Joined: Nov 23, 2008
Posts: 30
Once again, I'm an idiot, I took out the getModel code in the button class and everything worked fine.

Thanks again.

Chris
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Chris Nash wrote:Chris, Pete and I were telling you to do two different things. His suggestion is to extend JButton and implement your own paint routine. He says there is an isRollover() method you can call to check for rollover. In his case, you won't need a mouse listener.


Actually, I'm not a big fan of implementing his own paint routine as that steps on the look and feel's ability to paint the button. Rather, I thought that that was what the OP is trying to do. I guess what I/we really need is a detailed description of just what effect the OP is trying to create here because now I'm not so sure.

By the way, to the original poster, your latest code doesn't sit well with me, sorry. Again, I wouldn't use a MouseListener on a JButton or a descendant of a JButton. But more importantly, this is not kosher and looks to break easily:



You never call paintComponent directly.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Swing buttons support rollover directly. There's no need to add listeners or to fake it using a label component.

I don't have time for more details now, but look at the javadocs for AbstractButton (which is JButton's superclass) and/or ButtonModel.

bitguru blog
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Chris Baty wrote:It's easy if you use Icon but mine is custom drawn.

I missed this bit before my last post. However, there's no reason you can't do custom drawing in an Icon.

Icon is simply an interface with three methods:
  • int getIconHeight()
  • int getIconWidth()
  • void paintIcon(Component c, Graphics g, int x, int y)

  • So you simply implement this interface and do your custom drawing with the Graphics object passed into the paintIcon() method.
    (Unlike other Graphics painting you might have done, with Icon you start drawing at point <x,y>, not at point <0,0>.)

     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Creating mouse rollover for for custom drawn JButton
     
    Similar Threads
    JList Item Not Getting Mouse Clicks
    struts2 submit button rollover
    throwing exceptions in actionPerformed
    adding listeners
    difference between Swing, SWT and Jfaces?