0

如何更改只有一个 JList 单元格的 JLabel 图标(例如:单击 JList)?我尝试使用 listCellRender 访问 JLabel,但它不起作用:

    @Override
    public void valueChanged(ListSelectionEvent e) {
        if (!e.getValueIsAdjusting()) {
             ListCellRenderer r = list.getCellRenderer();
             JLabel comp = (JLabel) r.getListCellRendererComponent(list, list.getSelectedValue(), list.getSelectedIndex(), false, false);
             comp.setIcon(UIManager.getIcon("Tree.leafIcon"));
    }

任何人都可以给出一些想法吗?提前致谢!

我的代码:

import java.awt.*;
import java.util.Arrays;
import javax.swing.*;
import javax.swing.event.*;

/**
 * @see http://stackoverflow.com/questions/7620579
 * @see http://stackoverflow.com/questions/4176343
 */
public class ListPanel extends JPanel {

    private static final long serialVersionUID = -5558761237888601952L;
    private static final int N = 5;
    private DefaultListModel dlm = new DefaultListModel();
    private JList list = new JList(dlm);
    private ListRenderer myRender;
    // http://stackoverflow.com/questions/13752188/chat-client-emoticons-window-java
    // http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html VER

    public ListPanel() {
        super(new GridLayout());

        for (int i = 0; i < N * N; i++) {
            String name = "Cell: " + i;//String.format("%02d", i);
            dlm.addElement(name);
        }
        //dlm.getElementAt(0)
        list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        //list.getCellRenderer().getListCellRendererComponent(list, value, index, isSelected, cellHasFocus)
        list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
        list.setVisibleRowCount(N);
        myRender = new ListRenderer();
        list.setCellRenderer(myRender);
        list.addListSelectionListener(new SelectionHandler());
        JScrollPane pane = new JScrollPane(list);
        this.add(pane, BorderLayout.CENTER);
    }

    private class ListRenderer extends DefaultListCellRenderer {


        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            JLabel label =  (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            //System.out.println(">> " + index);
            label.setBorder(BorderFactory.createEmptyBorder(N, N, N, N));
            label.setIcon(UIManager.getIcon("html.pendingImage"));
            label.setHorizontalTextPosition(JLabel.CENTER);
            label.setVerticalTextPosition(JLabel.BOTTOM);
            return label;
        }
    }

    private class SelectionHandler implements ListSelectionListener {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                System.out.println(list.getSelectedValue());
            }
        }
    }

    private void display() {
        JFrame f = new JFrame("ListPanel");
        //f.setPreferredSize(new Dimension(400, 400));
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ListPanel().display();
            }
        });
    }
}
4

3 回答 3

2

你倒着做。单元格渲染器应该简单地使用isSelected参数并在此参数为真时设置您想要的图标,如果为假则不设置图标。

不需要选择监听器。The renderer will automatically be invoked when the selection changes.

于 2013-09-28T06:56:35.817 回答
2
  • 我认为有更简单的 public void valueChanged(ListSelectionEvent e) {方法ListSelectionListener

  • 看到getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {那里你可以覆盖isSelected,因为 SelectionModel 中的多选更好cellHasFocus

于 2013-09-28T06:56:38.443 回答
1

有没有办法在渲染后更改图标?

您可以在渲染时根据索引或值等标准动态选择图标。使用此处引用的示例,以下渲染器生成显示的图像。您可以随时更新图标和repaint().

列表面板图标

private class ListRenderer extends DefaultListCellRenderer {

    Icon icon1 = UIManager.getIcon("html.pendingImage");
    Icon icon2 = UIManager.getIcon("html.missingImage");

    public ListRenderer() {
        this.setBorder(BorderFactory.createLineBorder(Color.red));
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object
            value, int index, boolean isSelected, boolean cellHasFocus) {
        JLabel label = (JLabel) super.getListCellRendererComponent(
            list, value, index, isSelected, cellHasFocus);
        label.setBorder(BorderFactory.createEmptyBorder(N, N, N, N));
        label.setIcon(icon1);
        if (index % 2 == 0) {
            label.setIcon(icon2);
        }
        label.setHorizontalTextPosition(JLabel.CENTER);
        label.setVerticalTextPosition(JLabel.BOTTOM);
        return label;
    }
}
于 2013-09-30T10:48:10.033 回答