Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Image problem in SWING

 
Saurabh Agrawal
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends ,
I am a learner in SWING .. i have a problem which i would hope that atleast someone wud have the solution for it ..
"I am using a GRIDBAGLAYOUT and adding buttons and labels to a panel ..Its working fine but now i also wanna add an IMAGE to the panel .. how can i do that without using button or label as a container for image ?? please explaing with the code for it ??
"
Hoping for an early response.
saurabh
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no such thing as an IMAGE component... you *have* to use another component to display an image. You're not really specific about what you are trying to do, but if you need to load an image from a file, the easiest thing to do is load the image into an ImageIcon and then send it to a JLabel :



If you are trying to make some kind of dynamic image that the user has to interact with, then you would be better off making a custom component, using BufferedImage, etc. This is much more complicated...
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Nathan said you can add an image as a component and let the layout manager lay it out with the other components. The other general approach is to draw the image on the background of the panel that you are adding your components to — components then appear above the plane of the image.

[ March 11, 2004: Message edited by: Craig Wood ]
 
Saurabh Agrawal
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello NATHAN and CRAIG ,
Well thx for ur replies and it really helped me out .. i was actually implementing the thing as wat it was said by NATHAN ..so the conclusion is that we cant have image as a component and it needs a container like LABEL though label itsel fis a component ...
Anyways thx again and hope u keep giving ur suggestions always ,
saurabh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic