I have a JList in Java. Whenever the user clicks on an element, I would like to add a specific borderLine in that element. Is that possible?
I have the following code in Java:
DefaultListModel listModel = new DefaultListModel();
listModel.addElement("element1");
listModel.addElement("element2");
listModel.addElement("element3");
list = new JList(listModel);
list.addListSelectionListener(this);
For the listener i have:
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {
if (list.getSelectedIndex() == -1) {
;
} else {
CurrentIndex = list.getSelectedIndex();
//set Current's Index border, how can i do that?
}
}
}
You can use a custom
ListCellRender
. Then in theif (isSelected)
just add the border.Then instantiate the render.
The renderer will return a JLabel. So you can do anything you want to that label and that's what will appear in cell.
See
ListCellRenderer javadoc