2

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

Ps 对不起我的英语和语法;

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

1 回答 1

2

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

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

于 2013-10-22T15:26:50.623 回答