This week's book giveaway is in the Raspberry Pi forum.
We're giving away four copies of Getting started with Java on the Raspberry Pi and have Frank DelPorte on-line!
See this thread for details.
Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

Graphic JButon on JPanel

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a JButton that uses images for it's states (normal, disabled, rollover. etc.). I add it to a JPanel, but I can't see it. One big problem may be that I override the drawComponent() method in JPanel, to draw a background image, and I can't call the JButton's drawComponent() method explicitly (because it's protected).

I've checked all the obvious things:

* The images are fine--there isn't any error when loading them.
* I've set the button to visible.
* I add it to the JPanel

Any ideas? TIA!
 
Sheriff
Posts: 22755
130
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
drawComponent? I'm sure you meant paintComponent, right? You shouldn't need to call paintComponent of the JButton since it will be automatically called when you repaint the entire panel, unless you've overridden the panel's repaint method and forgot to call super.paint(g).

How about you show us the painting method?
 
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
Yes, I meant paintComponent() (my bad). I wasn't calling super.paint( g ), but now that I do, it crashes (actually, it goes into an endless loop of calling paintComponent() over and over again. If I didn't kill it, I assume it would eventually crash). Here is the code, as requested.



BTW, all the examples I've seen which override paintComponent() never call super.paint(g), which is why I never called it in the first place.
 
Rob Spoor
Sheriff
Posts: 22755
130
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll need to call super.paint(g) when you override paint (which you should only do if paintComponent is absent, like in AWT) and call super.paintComponent(g) when overriding paintComponent. So your code would become this:
 
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
Thank you for your help. Unfortunately, now I get a big white rectangle in the corner when I render, even when the panel is turned off. It's even in the wrong position, but is just the right size. The Image JButton still doesn't show up anywhere. Perhaps I'm doing something wrong elsewhere. I'll take a look.
 
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic