Reputation: 3987
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.
Upvotes: 1
Views: 4897
Reputation: 31
I found a solution which is at least 90% :)
1.My renderer does not extend ListCellRenderer, but ListCellRenderer<SomeClass>
. So the signature ot the relevant method is public Component getListCellRendererComponent(JList list, SomeClass value, int index, boolean isSelected, boolean cellHasFocus)
.
You see, the value is not Object.
2.There is no need for this method to return a JLabel. It could be any Component. So i set up a vertical Box with the JLabel as first contents, and a JSeparator as second. So there is no need to have a JSeparator as every second element in the list. The list has only the data objects to be shown.
This leads to another problem: The JLabel within that Box is reduced in size to the minimum. It is not easy to extend it to the full width. To set the JLabel width to a full width within a Box, you have to set the MaximumSize. To get the correct size, i use the size of the JLabel, keep the height, and use the width of the JSeparator, and set the JLabel maximum size to that value. Problem here is: On startup, the JSeparator has no useful size (0/0). So i wait with adjusting until i get a useful size here.
And here is the missing 10%: After startup, the first cell of the list is not size adjusted. It still displayes the JLabel with its minimum size. This gets corrected after the first data update, or when changing the displayed page once and back. I'm currently not sure how to fix this. maybe artificially fire a change event after the list is visible?
Addendum: I did an update for the first element after isValid()
returned true. isVisible()
is not good here.
Upvotes: 0
Reputation: 1796
The easiest thing I can think of is using a Table with one column (or if you need more), and setting the lines visible.
Upvotes: 0
Reputation: 458
You should use ListCellRenderer to add the separators at specific positions...
jList1 = new javax.swing.JList();
//String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
jList1.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
Vector v = makeVectorData(strings);
public int getSize() { return v.size(); }
public Object getElementAt(int i) { return v.get(i); }
});
jList1.setCellRenderer(new JlistRenderer());
jList1.addFocusListener(new JListFocusListener(jList1));
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...
Upvotes: 1