Java 在JList中呈现图标和文本的最佳方式

Java 在JList中呈现图标和文本的最佳方式,java,swing,jbutton,jlist,Java,Swing,Jbutton,Jlist,我创建了一个JList,它由我自己的AbstractListModel子类构造,模型存储Action类的实例,我将getElementAt()定义为 public final Object getElementAt(final int index) { return ((Action) actionList.get(index)).getValue(Action.NAME); } 我的JList显示操作名称列表,这是正常的 但是这些动作也

我创建了一个JList,它由我自己的AbstractListModel子类构造,模型存储Action类的实例,我将getElementAt()定义为

public final Object getElementAt(final int index)
        {
            return ((Action) actionList.get(index)).getValue(Action.NAME);
        }
我的JList显示操作名称列表,这是正常的

但是这些动作也定义了一个图标,所以如果我这样做

 public final Object getElementAt(final int index)
        {
            return ((Action) actionList.get(index)).getValue(Action.SMALL_ICON)
            );
        }
它现在显示的是图标

但我两个都想要,所以我试过了

 public final Object getElementAt(final int index)
        {
            return new JButton(
                    (String)((Action) actionList.get(index)).getValue(Action.NAME),
                    (Icon)((Action) actionList.get(index)).getValue(Action.SMALL_ICON)
            );
        }

现在,它只输出按钮的属性,而不是为什么不阅读javadoc帮助

getElementAt()应该是

public final Object getElementAt(final int index)
        {
            return actionList.get(index);
        }
然后我在javadoc中查看渲染,并进行如下修改:

class MyCellRenderer extends JLabel implements ListCellRenderer {
         ImageIcon longIcon = new ImageIcon("long.gif");
         ImageIcon shortIcon = new ImageIcon("short.gif");

        // This is the only method defined by ListCellRenderer.
        // We just reconfigure the JLabel each time we're called.

        public Component getListCellRendererComponent(
                JList list,              // the list
                Object value,            // value to display
                int index,               // cell index
                boolean isSelected,      // is the cell selected
                boolean cellHasFocus)    // does the cell have focus
        {
            Action action = (Action)value;
            setText((String)action.getValue(Action.NAME));
            setIcon((Icon)action.getValue(Action.SMALL_ICON));
            if (isSelected) {
                setBackground(list.getSelectionBackground());
                setForeground(list.getSelectionForeground());
            } else {
                setBackground(list.getBackground());
                setForeground(list.getForeground());
            }
            setEnabled(list.isEnabled());
            setFont(list.getFont());
            setOpaque(true);
            return this;
        }
    }
然后设置为Jlists渲染器

availableList.setCellRenderer(new MyCellRenderer());

而且它是有效的。

不要阅读javadoc帮助

getElementAt()应该是

public final Object getElementAt(final int index)
        {
            return actionList.get(index);
        }
然后我在javadoc中查看渲染,并进行如下修改:

class MyCellRenderer extends JLabel implements ListCellRenderer {
         ImageIcon longIcon = new ImageIcon("long.gif");
         ImageIcon shortIcon = new ImageIcon("short.gif");

        // This is the only method defined by ListCellRenderer.
        // We just reconfigure the JLabel each time we're called.

        public Component getListCellRendererComponent(
                JList list,              // the list
                Object value,            // value to display
                int index,               // cell index
                boolean isSelected,      // is the cell selected
                boolean cellHasFocus)    // does the cell have focus
        {
            Action action = (Action)value;
            setText((String)action.getValue(Action.NAME));
            setIcon((Icon)action.getValue(Action.SMALL_ICON));
            if (isSelected) {
                setBackground(list.getSelectionBackground());
                setForeground(list.getSelectionForeground());
            } else {
                setBackground(list.getBackground());
                setForeground(list.getForeground());
            }
            setEnabled(list.isEnabled());
            setFont(list.getFont());
            setOpaque(true);
            return this;
        }
    }
然后设置为Jlists渲染器

availableList.setCellRenderer(new MyCellRenderer());

它可以工作。

您可以使用基于
JLabel
DefaultListCellRenderer
,它将为您处理大部分工作,您只需根据需要调用
setText
setIcon
。您可以使用基于
JLabel
DefaultListCellRenderer
,它将为您处理大部分工作,您只需根据需要调用
setText
setIcon