aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JPanel" Watch "JPanel" New topic
Author

JPanel

Jude Fawly
Ranch Hand

Joined: Dec 01, 2004
Posts: 33
hi,

how can i use a JPanel in a way to be able to draw on it in the first place and then add components to it?

thanks.


Teach me to die that so I may<br />Rise glorious at the awful day
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Subclass JPanel and override paintComponent:

I've written plenty of custom JComponents that paint graphics,
but I've never had to combine graphics with being a container with children.
What are you trying to do?


There is no emoticon for what I am feeling!
Jude Fawly
Ranch Hand

Joined: Dec 01, 2004
Posts: 33
hi Jeff,

thanks for your reply. i am trying have a GUI frame, for example a login screen, that has a nice image on the background, but on the same time this GUI would have textfields and buttons on them.
It is possible?

thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Yes, it's possible, but I think the code should be more like this:
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

I did this a while ago, see here.

You don't need to set the panel to opaque, or draw the image first - the controls take precedence over the image.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
henri henri
Ranch Hand

Joined: Oct 03, 2005
Posts: 115
you could just make a subclass of JPanel that takes an Image as I have done. Any Components will be placed overtop the image.

In whatever class you want to use it in, you simply write ImagePanel = new ImagePanel(img);
henri henri
Ranch Hand

Joined: Oct 03, 2005
Posts: 115
sorry, please look at the corrected code. Good luck!

ImagePanel panel = new ImagePanel(img);
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Don't forget to call super.paintComponent(g), unless you explicitly do not want that (but most of the times, you do).
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JPanel