Hey, how do i make my java SWT list look like here http://flavio.tordini.org/minitunes I mean that each element is separated by a line from eachother. Or i there any other solution to have a list like in the minitunes, so i can add number.. etc.
Hey, how do i make my java SWT list look like here http://flavio.tordini.org/minitunes I
Share
You should use ListCellRenderer to add the separators at specific positions…
public class JlistRenderer extends JLabel implements ListCellRenderer {
JSeparator separator;
final String SEPARATOR = "SEPARATOR";
public JlistRenderer() {
setOpaque(true);
setBorder(new EmptyBorder(1, 1, 1, 1));
separator = new JSeparator(JSeparator.HORIZONTAL);
}
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
String str = (value == null) ? "" : value.toString();
if (SEPARATOR.equals(str)) {
return separator;
}
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setFont(list.getFont());
setText(str);
return this;
}
}
public class JListFocusListener implements FocusListener {
JList list;
Object currentItem;
final String SEPARATOR = "SEPARATOR";
JListFocusListener(JList list) {
this.list= list;
list.setSelectedIndex(0);
currentItem = list.getSelectedValue();
}
public void focusGained(FocusEvent e) {
String tempItem = (String) list.getSelectedValue();
if (SEPARATOR.equals(tempItem)) {
list.setSelectedValue(currentItem,true);
} else {
currentItem = tempItem;
}
}
}
Hope the above code helps...