| Author |
ImageIcon returned from Event Handler
|
Marc Kryzhan
Greenhorn
Joined: Mar 17, 2004
Posts: 14
|
|
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 ]
|
Newbee to java.
|
 |
Craig Wood
Ranch Hand
Joined: Jan 14, 2004
Posts: 1535
|
|
It looks like this AutoImageUtils.getImage("Z8") returns an ImageIcon which could be the argument of pix.setIcon(). Anytime you update a JLabel you may have to call revalidate (especially if the size of the icon or string change the labels size) and/or repaint. Sometimes you need one or the other and sometimes both, revalidate followed by repaint.
|
 |
Marc Kryzhan
Greenhorn
Joined: Mar 17, 2004
Posts: 14
|
|
Thanks for your help. I am now able to get one ImageIcon to display in the correct container. I get the ImageIcon, that is represented by "ZO6", to appear in the correct container. [B]My Problem is I'm not able to cast in ,[/B] in place of "Z06" because this method will not take a type Object. My method in AutoInventoryModel (grabbed as new instance represented by autoInventoryModel) that I'm calling to grab the row and column of the JTable is: Thanks for any help. Marc [ April 08, 2004: Message edited by: Marc Kryzhan ] [ April 08, 2004: Message edited by: Marc Kryzhan ] [ April 08, 2004: Message edited by: Marc Kryzhan ] [ April 08, 2004: Message edited by: Marc Kryzhan ] [ April 08, 2004: Message edited by: Marc Kryzhan ] [ April 08, 2004: Message edited by: Marc Kryzhan ] [ April 08, 2004: Message edited by: Marc Kryzhan ] [ April 08, 2004: Message edited by: Marc Kryzhan ] [ April 08, 2004: Message edited by: Marc Kryzhan ] [ April 08, 2004: Message edited by: Marc Kryzhan ]
|
 |
 |
|
|
subject: ImageIcon returned from Event Handler
|
|
|