• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPanel

 
Jude Fawly
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jude Fawly
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20978
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it's possible, but I think the code should be more like this:
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, please look at the corrected code. Good luck!

ImagePanel panel = new ImagePanel(img);
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't forget to call super.paintComponent(g), unless you explicitly do not want that (but most of the times, you do).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic