带有图像和文本的 JList:其中文本来自 ArrayList

发布于 2024-11-28 15:56:20 字数 2478 浏览 3 评论 0原文

我有一个简单的 JList 示例,它从 ArrayList 获取数据,但我想在列表中每个字符串旁边显示一个图像。我编写了一个自定义单元格渲染器(IconListRenderer),它应该并排显示图标和对象。

这是一个正在运行的示例。

//Test class showing the list in a frame
import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
import javax.swing.border.LineBorder;


public class Test extends JFrame{

     public static void main(String[] args) {


    final JFileChooser chooser = new JFileChooser();
    JButton button = new JButton();
    button.setText("Upload");
    JFrame frame = new JFrame("My Frame");
    JList list = new JList();
    Map<Object, ImageIcon> icons = new HashMap<Object, ImageIcon>();
    list.setBorder(new LineBorder(Color.BLACK));

    ImageIcon icon = new ImageIcon("/Images/400px-Greek_uc_sigma.png");

    ArrayList<String> arrayList = new ArrayList<String>();

    icons.put("Name", icon);

    //populate the arrayList for testing
    arrayList.add("Smith");
    arrayList.add("John");
    arrayList.add("Bob");
    arrayList.add("Kim");

    frame.setSize(new Dimension(400, 400));
    //set the list data
    list.setListData(arrayList.toArray());
    final JFrame imageFrame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    list.setCellRenderer(new IconListRenderer(icons));
    frame.add(list); 
    frame.setVisible(true);
    frame.repaint();
}



}

//IconListRenderer类

import java.awt.Component;
import java.util.Map;
import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;


 public class IconListRenderer
                           extends DefaultListCellRenderer {

private Map<Object, ImageIcon> icons = null;

public IconListRenderer(Map<Object, ImageIcon> 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 simple example of a JList that is getting it's data from an ArrayList but i want to show an image next to each string in the list. I have written a custom cell renderer (IconListRenderer) that is suppose to display an icon and the object side-to-side.

Here is a running sample.

//Test class showing the list in a frame
import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
import javax.swing.border.LineBorder;


public class Test extends JFrame{

     public static void main(String[] args) {


    final JFileChooser chooser = new JFileChooser();
    JButton button = new JButton();
    button.setText("Upload");
    JFrame frame = new JFrame("My Frame");
    JList list = new JList();
    Map<Object, ImageIcon> icons = new HashMap<Object, ImageIcon>();
    list.setBorder(new LineBorder(Color.BLACK));

    ImageIcon icon = new ImageIcon("/Images/400px-Greek_uc_sigma.png");

    ArrayList<String> arrayList = new ArrayList<String>();

    icons.put("Name", icon);

    //populate the arrayList for testing
    arrayList.add("Smith");
    arrayList.add("John");
    arrayList.add("Bob");
    arrayList.add("Kim");

    frame.setSize(new Dimension(400, 400));
    //set the list data
    list.setListData(arrayList.toArray());
    final JFrame imageFrame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    list.setCellRenderer(new IconListRenderer(icons));
    frame.add(list); 
    frame.setVisible(true);
    frame.repaint();
}



}

//IconListRenderer class

import java.awt.Component;
import java.util.Map;
import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;


 public class IconListRenderer
                           extends DefaultListCellRenderer {

private Map<Object, ImageIcon> icons = null;

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

The list shows currently but no image?

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

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

发布评论

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

评论(1

﹉夏雨初晴づ 2024-12-05 15:56:21

这并不奇怪:

  • 您使用 JList 项值作为键从图标映射中获取图标;
  • 图标映射包含一个键:“Name”
  • JList 包含“Smith”、“John”、“Bob”和“Kim”,但没有“Name”

It's not really surprising:

  • you use the JList item value as a key to get the icon from the icon map;
  • the icon map contains a single key: "Name"
  • the JList contains "Smith", "John", "Bob" and "Kim", but no "Name"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文