1

i have a simple example of a JList that is getting it's data from an ArrayList but i want to show an image next to each string in the list. I have written a custom cell renderer (IconListRenderer) that is suppose to display an icon and the object side-to-side.

Here is a running sample.

//Test class showing the list in a frame
import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
import javax.swing.border.LineBorder;


public class Test extends JFrame{

     public static void main(String[] args) {


    final JFileChooser chooser = new JFileChooser();
    JButton button = new JButton();
    button.setText("Upload");
    JFrame frame = new JFrame("My Frame");
    JList list = new JList();
    Map<Object, ImageIcon> icons = new HashMap<Object, ImageIcon>();
    list.setBorder(new LineBorder(Color.BLACK));

    ImageIcon icon = new ImageIcon("/Images/400px-Greek_uc_sigma.png");

    ArrayList<String> arrayList = new ArrayList<String>();

    icons.put("Name", icon);

    //populate the arrayList for testing
    arrayList.add("Smith");
    arrayList.add("John");
    arrayList.add("Bob");
    arrayList.add("Kim");

    frame.setSize(new Dimension(400, 400));
    //set the list data
    list.setListData(arrayList.toArray());
    final JFrame imageFrame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    list.setCellRenderer(new IconListRenderer(icons));
    frame.add(list); 
    frame.setVisible(true);
    frame.repaint();
}



}

//IconListRenderer class

import java.awt.Component;
import java.util.Map;
import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;


 public class IconListRenderer
                           extends DefaultListCellRenderer {

private Map<Object, ImageIcon> icons = null;

public IconListRenderer(Map<Object, ImageIcon> icons) {
    this.icons = icons;
}

@Override
public Component getListCellRendererComponent(
    JList list, Object value, int index,
    boolean isSelected, boolean cellHasFocus) {

    // Get the renderer component from parent class

    JLabel label =
        (JLabel) super.getListCellRendererComponent(list,
            value, index, isSelected, cellHasFocus);

    // Get icon to use for the list item value

    Icon icon = icons.get(value);

    // Set icon to display for value

    label.setIcon(icon);
    return label;
}
  }

The list shows currently but no image?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Warz
  • 7,386
  • 14
  • 68
  • 120
  • 1
    See how this works. You post a SSCCE and get an answer within a couple of minutes. You asked the same question a week ago (http://stackoverflow.com/questions/6884973/displaying-an-imageicon-in-a-jlist-that-uses-a-different-object-to-load-the-jlist). I could have given you the answer then, but you refused to post a SSCCE so I refused to answer. – camickr Aug 07 '11 at 02:19
  • Well put. I didn't refuse to put up an SSCCE just that i did not fully know how to make a small example without posting an extensive amount of my code. – Warz Aug 07 '11 at 16:37
  • And that is my point about a SSCCE. If you don't know how to simplify the code, then you don't really understand the question you are asking. As you now see the StudentListener class was completely irrelevant to the problem and you can't solve problems if you look in the wrong places. Now that you know how to create a SSCCE I expect we will see more in the future :) – camickr Aug 07 '11 at 18:03

1 Answers1

2

It's not really surprising:

  • you use the JList item value as a key to get the icon from the icon map;
  • the icon map contains a single key: "Name"
  • the JList contains "Smith", "John", "Bob" and "Kim", but no "Name"
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • okay i added that to my list and the icon does show up now. Do you know how to set the size of the icon that shows up on the list. – Warz Aug 06 '11 at 22:56
  • The size of the icon is the size of the underlying png image. Just reduce the image if it's too large. – JB Nizet Aug 06 '11 at 23:02