Displaying an ImageIcon in a JList that uses a different Object to load the JList data

夙愿已清 提交于 2020-01-15 12:16:06

问题


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).

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

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;
}
  }

回答1:


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



来源:https://stackoverflow.com/questions/6884973/displaying-an-imageicon-in-a-jlist-that-uses-a-different-object-to-load-the-jlis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!