Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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: 22649
126
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: 22649
126
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.
 
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic