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

时间:2013-10-22 15:17:51

标签: java override jlist point defaultlistmodel

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

P.S。对不起我的英语和语法;

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());
        }
    }
}

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

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

1 个答案:

答案 0 :(得分:2)

  

我不能只重写Point的toString()。

您需要使用自定义渲染器。阅读How to Use Lists上的Swing教程中的Writing a Custom Renderer部分。