在 JList 中显示使用不同对象加载 JList 数据的 ImageIcon

发布于 2024-11-26 21:45:31 字数 2586 浏览 1 评论 0 原文

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

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

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

是我的动作监听器,它将数据添加到列表中。IconListCellRenderer

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧话新听 2024-12-03 21:45:31

JList 具有如何将 Icon/ImageIcon 添加到 ListCellRenderer,例如链接是关于 JComboBox 包含 JList,另一个示例 此处此处

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文