I am puzzled about getting an
ImageIcon to appear in one container when a row is selected in a scrollable Jtable from another container (I easily get text to appear in the container):
My setup:
- I have a new JLabel called pix inside a JPanel called PixContainer.
- I also have JTable called AutoScrollTable which is defined as a JScrollPane inside a JPanel called AutoContainer. The table has 4 columns of information taken from an xml document. Important from the xml document is a model type which is the key to 7 pieces of information. One of the seven is the name of a .jpg for a vehicle.
- I have some images (10 of them) of different sports cars and I already created ImageIcons out of them.
My two methods in a class called AutoImageUtils, one to set up and one to hopefully grab the imageicons:
// To get image icon with a key I provide from my Event Handler
public static ImageIcon getImage (String key) {
ImageIcon ii;
ii = (ImageIcon) autoImages.get(key);
int siz = autoImages.size();
System.out.println("8 888888888 AutoImagesUtils::getImage key/ii/size= " + key + " " + ii + " " + siz );
return ii;
}
//* Returns an ImageIcon, or null if the path was invalid. Name is resolved to a URL.*//
private static ImageIcon createImageIcon(String path, String description) {
String fullPath = "../images/" + path;
java.net.URL imgURL = AutoImageUtils.class.getResource(fullPath);
if (imgURL != null) {
System.out.println("createImageIcon:URL " + imgURL);
return new ImageIcon(imgURL, description);
} else {
System.out.println("Couldn't find file " + fullPath + " with URL " + imgURL);
return null;
}
My event handler which is where I'm having trouble is in a separate class which also contains the containers, my Jlabel pix, and my Jtable:
class ListSelectionHandler implements ListSelectionListener {
public void valueChanged(ListSelectionEvent e) {
// column "1" of the JTable is the model (or type).
final int TYPE_COL = 1;
// get the index (row number) that is selected in the JTable
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
int selectedRow = lsm.getMinSelectionIndex(); //the current selection
System.out.println("ListSelectionHandler() Index/row= " + selectedRow);
//
Problem One: using this row number, I want to get the key that is used to find the image. // Like I said the key is the model(type) because it's unique.
//* Below I forced in a key by using the model type of one auto, the Z8. My return from getImage was expected [8 888888888 AutoImagesUtils::getImage key/ii/size= Z8 z8.jpg 10] *//
AutoImageUtils.getImage("Z8");
//*
Problem Two: I want to get the image that has been pre-loaded in AutoImageUtils. Hopefully I can return an ImageIcon *//
[I believe all I need at this point is to give a visual attribute to Jlabel but I�m having no success].
//*I believe I must also update the JLabel component to render this image (JLabel setIcon())*//
pix.setIcon(images[AutoImageUtils.getImage("Z8")];
pix.setIcon("hello");
My code for the Jlabel (which is bring back simple text currently):
pix = new JLabel("my name is marc");
JPanel pixContainer = new JPanel(new GridLayout(1,1));
pixContainer.setBorder(BorderFactory.createTitledBorder("Image of the Sports Car"));
pixContainer.add(pix); // add the image to the container
Thanks for any suggestions or pointers (not C pointers).
Marc
[ April 07, 2004: Message edited by: Marc Kryzhan ]