This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Graphic JButon on JPanel

 
Chris Nash
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • 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!
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic