// Display an icon and a string for each object in the list. 
 class MyCellRenderer extends JLabel implements ListCellRenderer {
     final static ImageIcon longIcon = new ImageIcon("long.gif");
     final static ImageIcon shortIcon = new ImageIcon("short.gif");     // This is the only method defined by ListCellRenderer.
     // We just reconfigure the JLabel each time we're called.     public Component getListCellRendererComponent(
       JList list,
       Object value,            // value to display
       int index,               // cell index
       boolean isSelected,      // is the cell selected
       boolean cellHasFocus)    // the list and the cell have the focus
     {
         String s = value.toString();
         setText(s);
         setIcon((s.length() > 10) ? longIcon : shortIcon);
       if (isSelected) {
             setBackground(list.getSelectionBackground());
       setForeground(list.getSelectionForeground());
   }
         else {
       setBackground(list.getBackground());
       setForeground(list.getForeground());
   }
   setEnabled(list.isEnabled());
   setFont(list.getFont());
         setOpaque(true);
         return this;
     }
 } String[] data = {"one", "two", "three", "four"};
 JList dataList = new JList(data);
 dataList.setCellRenderer(new MyCellRenderer());document里面的

解决方案 »

  1.   

    我认为可以定义一个对象类,将你的图片和字符串捆绑在一起,然后将这个对象作为list的item.
      

  2.   

    JList有个传递Vector类的构造器,你可以把图片和字符串等任何类先放入一个Vector中,再调用JList(Vector v)构造器,记住从Vector中取出对象时要用该对象类型还原就行了,如:Vector v =  new Vector();
    String s = "Hello";
    v.addItem(s);
    JList l = new JList(v);
    ...
    String s = (String) v.item(0);// 可能不是item(),我忘了