【问题标题】:Displaying an ImageIcon in a JList that uses a different Object to load the JList data在使用不同对象加载 JList 数据的 JList 中显示 ImageIcon
【发布时间】:2011-10-16 15:09:30
【问题描述】:

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

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

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

这是我将数据添加到列表的操作侦听器。

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

【问题讨论】:

  • 您还没有发布 SSCCE!我们无权访问您的 StuentModel 或 IconListRenderer。我们甚至不需要 SSCCE 的 StudentModel。您需要做的就是在 ArrayList 中硬编码一些学生姓名。我猜问题出在渲染器上。 如果您需要帮助,请发布适当的 SSCCE。我厌倦了每次你发布问题时都会提醒你。
  • 我必须同意 camickr:如果没有一些小的工作示例,我们真的无法猜测为什么您的代码无法正常工作。如果您想提供 SSCCE,这完全取决于您——您需要我们多少帮助?
  • @Camickr,不需要动画我真的很接近解决这个问题,并认为我可能只是没有正确使用 ListCellRenderer 属性,这就是我发布引用代码的原因。我会更新我的帖子以获得更好的回应。
  • @Warz,显然我确实需要变得生动起来,因为您仍然没有理解您通过不发布适当的问题来浪费我们时间的概念将解决问题所需的所有信息。到目前为止,您已经浪费了 21 个花时间查看问题的人的时间。根据定义,当您遇到问题时,您不知道导致问题的原因,因此您不知道与发布相关的代码,这就是您通过创建 SSCCE 来缩小问题范围的原因。
  • @Warz,您还没有发布 SSCCE。更新的代码没有帮助。添加硬编码数据,当我们无法执行程序时仍然无济于事。硬编码数据意味着您创建一个框架并显示列表,以便我们可以看到您在说什么以及渲染器实际在做什么。你还在浪费大家的时间。

标签: java swing jlist listcellrenderer imageicon


【解决方案1】:

JList有方法如何将Icon/ImageIcon添加到ListCellRenderer,例如链接是关于包含JListJComboBox,另一个例子herehere

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-16
    相关资源
    最近更新 更多