在使用不同对象加载JList数据的JList中显示ImageIcon [英] Displaying an ImageIcon in a JList that uses a different Object to load the JList data

查看:153
本文介绍了在使用不同对象加载JList数据的JList中显示ImageIcon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JList,该JList通过其他地方的字符串ArrayList填充,我想针对同一列表现在显示保存在目录中的ImageIcon.现在,我想为添加到列表中的任何项目(或列表中当前的任何项目)显示相同的图标.

I have a JList that is being populated through an ArrayList of strings somewhere else, i want to for the same list now display an ImageIcon saved in my directory somewhere. For now i want to display the same icon for any item added to the list (or any items currently in the list).

我的列表应如下所示: ICON STUDENT NAME ... 图标学生姓名

My list should look like this : ICON STUDENT NAME ... ICON STUDENT NAME

问题(图像图标显示了正确的高度,已被捕获,但在运行时未显示在列表中

The problem (The image icon shows the correct height and it is being captured but does not show in the list at run-time

这是我的动作侦听器,它将数据添加到列表中.

Here is my action listener that adds the data to the List.

 public class StudentListener implements ActionListener{

   private Main_Menu menu;
   private ArrayList<String> arrayList = new ArrayList<String>();;
   Iterator iterator = arrayList.iterator();
   JList sList;
   Map<Object, Icon> icons = new HashMap<Object, Icon>();        
   /**
    * 
    * @param menu the referenced menu from our main menu
    */
   public StudentListener(Main_Menu menu){
   this.menu = menu;       
   }

   @Override
    public void actionPerformed(ActionEvent ae) {

    Icon iCon = new ImageIcon("/Project/src/Images/1312046124_picture.png"); // icons
    int iHeight = iCon.getIconHeight();
       icons.put("name", iCon);           
      //add all the students to our List 
          try {
                StudentModel = new Student_Model();
            } catch (SQLException ex) {
                Logger.getLogger(Student_Controller.class.getName()).log(Level.SEVERE, null, ex);
            }
    //arrayList = StudentModel.getStudents(); // modify to use an arrayList of string
    arrayList.add("John");
    arrayList.add("Smith");
    iterator = arrayList.iterator();
    while(iterator.hasNext()){          
       System.out.println(iterator.next().toString());
    }
    sList = this.menu.getStudentList();
    sList.setListData(arrayList.toArray());
    sList.setFont(new Font("Arial", Font.BOLD, 14));
    System.out.println("height of icon " + iHeight); // displays the correct height
    sList.setCellRenderer(new IconListRenderer(icons));       
   }   
  }

IconListCellRenderer

IconListCellRenderer

public class IconListRenderer
extends DefaultListCellRenderer {

private Map<Object, Icon> icons = null;

public IconListRenderer(Map<Object, Icon> 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;
}
  }

推荐答案

JList具有如何将Icon/ImageIcon添加到JList的.html#renderer"rel =" nofollow> JComboBox ,另一个示例此处此处

JList has method how to add Icon/ImageIcon to the ListCellRenderer, link for example is about JComboBox that contains JList, another examples here and here

这篇关于在使用不同对象加载JList数据的JList中显示ImageIcon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆