• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating mouse rollover for for custom drawn JButton

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I'll give it a go.
Chris
 
Chris Baty
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Brian Cole
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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>.)

     
    reply
      Bookmark Topic Watch Topic
    • New Topic