i can't just override Point's toString().
You need to use a custom renderer. Read the Writing a Custom Renderer section from the Swing tutorial on How to Use Lists
.
Question
Good day everyone! I have few question: how i can override my JList's method, i have some array of Point, and they like walk between server and client side, so i can't just override Point's toString(). And my logic tell me, that i can do it in my JList or DefaultListModel, but i can't find information (probably i don't know how google that). So maybe anybody know how do it?
P.s. sorry for my English and grammar;
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());
}
}
}
My answer: (I don't want create new class, cos' this is few code)
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;
}
};
}
}
Solution
i can't just override Point's toString().
You need to use a custom renderer. Read the Writing a Custom Renderer section from the Swing tutorial on How to Use Lists
.