displaying image in jlabel

when displaying an image in a jlable. the jlabel takes on the size of the image. i have tried to set the maximum bounds to the size of the original size of the jlable and even the panel the jlable sits on, but each time that i go to display the picture, it is displayed huge and not the size of the jlabel it is placed in.

here is my creation code, my gui was created in netbeans.

pnImage.setBorder(javax.swing.BorderFactory.createLineBorder(new

java.awt.Color(0, 0, 0)));

pnImage.setFocusable(false);

pnImage.setMaximumSize(new java.awt.Dimension(395, 365));

lblImage.setFocusable(false);

lblImage.setMaximumSize(new java.awt.Dimension(395, 365));

javax.swing.GroupLayout pnImageLayout =new javax.swing.GroupLayout(pnImage);

pnImage.setLayout(pnImageLayout);

pnImageLayout.setHorizontalGroup(

pnImageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(lblImage, javax.swing.GroupLayout.PREFERRED_SIZE, 393, javax.swing.GroupLayout.PREFERRED_SIZE)

);

pnImageLayout.setVerticalGroup(

pnImageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(lblImage, javax.swing.GroupLayout.PREFERRED_SIZE, 363, javax.swing.GroupLayout.PREFERRED_SIZE)

);

and here is how i display it.

Dimension d = pnImage.getSize();

System.out.println("d = " + d.toString());

pnImage.setMaximumSize(d);

lblImage.setMaximumSize(d);

ImageIcon icon =new ImageIcon(contact.getImage());

lblImage.setIcon(icon);

[1850 byte] By [developprogramsa] at [2007-11-27 0:13:30]
# 1

The Image class has a method getScaledInstance() that may help.

Is contact.getImage() an Image? If so find its width and height, then determine

a scale factor that will fit the image within your maximum desired width

and height. Finally create the icon using getScaledInstance().

pbrockway2a at 2007-7-11 21:57:21 > top of Java-index,Java Essentials,New To Java...
# 2
the contact.getImage() is a byte[]. i looked for ways to convert the byte[] into an image but found nothing.do you know of a way to do this?thanks for your help
developprogramsa at 2007-7-11 21:57:21 > top of Java-index,Java Essentials,New To Java...
# 3

I've never done this (convert a byte array to an image), so I thought I'd have a go...import java.awt.Image;

import java.awt.image.BufferedImage;

import java.io.ByteArrayInputStream;

import java.io.File;

import java.io.FileInputStream;

import javax.imageio.ImageIO;

import javax.swing.ImageIcon;

public class ImageTest {

public static void main(String[] args) throws Exception {

// Grab some image...

File inFile = new File("resources/imageH.jpg");

int len = (int)inFile.length();

System.out.printf("file length = %d%n", len);

FileInputStream in = new FileInputStream(inFile);

// ... and create a byte array for testing

byte[] byteArray = new byte[len];

int num = in.read(byteArray);

in.close();

System.out.printf("bytes read = %d%n", num);

// (1) Create an image from the byte array - I don't

// know Imagio figures out what sort of image it is!

BufferedImage im = ImageIO.read(new ByteArrayInputStream(byteArray));

int w = im.getWidth();

int h = im.getHeight();

System.out.printf("width = %d, height = %d", w, h);

// (2) Scale to fit 100x100

if(w > h) {

w = (100 * w) / h;

h = 100;

} else {

h = (100 * h) / h;

w = 100;

}

System.out.printf(" --> %d,%d%n", w, h);

// (3) And create the icon

ImageIcon icon = new ImageIcon(im.getScaledInstance(w, h, Image.SCALE_SMOOTH));

System.out.printf("The icon is: %s%n", icon);

}

}

The output isfile length = 54918

bytes read = 54918

width = 500, height = 300 --> 166,100

The icon is: javax.swing.ImageIcon@5afd29Assuming your byte array is some (to ImageIO) recognisable image format, something

similar should work for you.

pbrockway2a at 2007-7-11 21:57:21 > top of Java-index,Java Essentials,New To Java...
# 4
That worked well. Thanks for your help.
developprogramsa at 2007-7-11 21:57:21 > top of Java-index,Java Essentials,New To Java...