Click to See Complete Forum and Search --> : Using Image inside a Swing GUI


headshox
12-01-2004, 01:04 PM
Hi guys,

What is the 'correct' method for using images inside a Swing GUI?

Bascially I want to break this down into 2 questions:

1) How should they be stored, is it preferable to have the image hard-coded into code or as a seperate file?

2) How can you place them onscreen? At the moment I'm trying to do something as simple as making an "About" box with an image on the left and a bit of jLabel text on the right, however the image seems to get overlayed on top of everything else hence not obeying layout rules and covering up the text even though they are tied to different jPanels.

Cheers.

doredson
12-01-2004, 02:34 PM
1) you should be able to store images in a jar (perhaps the same one your class files are in). I would google for information about that.

2) What about putting your image as the background of a JPanel, setting the panel's size, and placing the panel in the layout? There must be some existing about dialog box code in Sun's tutorials.

doredson
12-01-2004, 02:37 PM
maybe you could read images in from your jar using java.util.jar.JarInputStream. Unless, of course, there is an even easier method of doing it.

headshox
12-01-2004, 02:58 PM
How is it possible to set the background of a jPanel to an image? The setBackground method only takes a colour as an argument.

doredson
12-01-2004, 03:11 PM
can you post your current code that overlays the text/image?