JList 覆盖单元格的 toString 以正确显示



大家好!我有几个问题:我如何覆盖我的 JList 方法,我有一些 Point 数组,他们喜欢在服务器端和客户端之间行走,所以我不能只覆盖 Point 的 toString()。我的逻辑告诉我,我可以在我的JList或DefaultListModel中做到这一点,但我找不到信息(可能我不知道如何谷歌)。所以也许有人知道怎么做?

附言对不起我的英语和语法;

public class PointsList extends JPanel {
    private Extra extra;
    private JList<Point> pointsList;
    private DefaultListModel<Point> listModel;
    public PointsList(Extra extra) {
        this.extra = extra;
        setBackground(Settings.pointListBackround);
        listModel = new DefaultListModel<Point>();
        pointsList = new JList<Point>(
               //Which method i must override, to change cell toString
        };
        pointsList.getSelectionModel().addListSelectionListener(getSelectionListAction());
        setLayout(new BorderLayout());
        add(pointsList);
        setPreferredSize(new Dimension(150, 100));
    }
    private ListSelectionListener getSelectionListAction() {
        return new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (e.getValueIsAdjusting()) {
                    extra.setSelectedPoint(pointsList.getSelectedValue());
                    extra.repaintCanvas();
                }
            }
        };
    }
    public void updateList() {
        listModel.removeAllElements();
        for (Map.Entry<Point, Boolean> currentPoint : extra.getPoints().entrySet()) {
            listModel.addElement(currentPoint.getKey());
        }
    }
}

我的回答:(我不想创建新类,因为这是几个代码)

public class PointsList extends JPanel {
    private Extra extra;
    private JList<Point> pointsList;
    private DefaultListModel<Point> listModel;
    public PointsList(Extra extra) {
        this.extra = extra;
        setBackground(Settings.pointListBackround);
        listModel = new DefaultListModel<Point>();
        pointsList = new JList<Point>(listModel);
        pointsList.setCellRenderer(getRender());
        pointsList.getSelectionModel().addListSelectionListener(getSelectionListAction());
        setLayout(new BorderLayout());
        add(pointsList);
        setPreferredSize(new Dimension(150, 100));
    }
    private ListSelectionListener getSelectionListAction() {
        return new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (e.getValueIsAdjusting()) {
                    extra.setSelectedPoint(pointsList.getSelectedValue());
                    extra.repaintCanvas();
                }
            }
        };
    }
    public void updateList() {
        listModel.removeAllElements();
        for (Map.Entry<Point, Boolean> currentPoint : extra.getPoints().entrySet()) {
            listModel.addElement(currentPoint.getKey());
        }
    }
    private ListCellRenderer<Point> getRender() {
        return new ListCellRenderer<Point>() {
            @Override
            public Component getListCellRendererComponent(JList<? extends Point> list, Point value, int index, boolean isSelected, boolean cellHasFocus) {
                JLabel cell = new JLabel("(" + value.x + " , " + value.y + ")");
                cell.setForeground(Color.black);
                return cell;
            }
        };
    }
}

我不能只覆盖 Point 的 toString()。

您需要使用自定义渲染器。阅读 Swing 教程中的编写自定义渲染器部分 How to Use Lists

相关内容

  • 没有找到相关文章

最新更新