It's not a secret anymore!
The moose likes Swing / AWT / SWT and the fly likes Image problem in SWING Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Image problem in SWING" Watch "Image problem in SWING" New topic

Image problem in SWING

Saurabh Agrawal
Ranch Hand

Joined: Oct 07, 2003
Posts: 244
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.

Success is not doing extraordinary things but doing ordinary things extraordinarily well.
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

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...

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
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

Joined: Oct 07, 2003
Posts: 244
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 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 ,
I agree. Here's the link:
subject: Image problem in SWING
It's not a secret anymore!