aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Graphic JButon on JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Graphic JButon on JPanel" Watch "Graphic JButon on JPanel" New topic
Author

Graphic JButon on JPanel

Chris Nash
Greenhorn

Joined: May 20, 2009
Posts: 28
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!

- Frecklefoot
"What you are good at is not as important as being good at something." --Robert B.Parker
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

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?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Chris Nash
Greenhorn

Joined: May 20, 2009
Posts: 28
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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

Joined: May 20, 2009
Posts: 28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Graphic JButon on JPanel