Split list in Java / Swing?
I have a list of items in JList
for a user to select. Since it has a lot of items (like cities within states), I want to split the list into sections. However, section titles should not be selectable. So for my cities / states example, it might look like this:
- Condition 1
- City 1
- City 2
- City 3
- Condition 2
- City 4
- City 5
- City 6
It wouldn't be that hard to write this myself by pasting JList
in a custom one ListCellRenderer
, but I'm wondering if such a class already exists there.
a source to share
I see that this question has already been answered, but I noticed Robert commented that he hopes for an open source solution. I would recommend using the Separator Glazed Lists, the API for which you can find here:
http://publicobject.com/glazedlists/glazedlists-1.8.0/api/ca/odell/glazedlists/SeparatorList.html
Here is some sample code that will create a list of items grouped by their first letter:
alt text http://img300.imageshack.us/img300/8977/separatorlist.png
public class SeparatorListTest {
private static Comparator<String> createComparator() {
return new Comparator<String>() {
public int compare(String stringOne, String stringTwo) {
return stringOne.substring(0,1).compareTo(stringTwo.substring(0,1));
}
};
}
private static ListCellRenderer createListCellRenderer() {
return new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value instanceof SeparatorList.Separator) {
SeparatorList.Separator separator = (SeparatorList.Separator) value;
label.setText(separator.getGroup().get(0).toString().substring(0,1));
label.setFont(label.getFont().deriveFont(Font.BOLD));
label.setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
} else {
label.setFont(label.getFont().deriveFont(Font.PLAIN));
label.setBorder(BorderFactory.createEmptyBorder(0,15,0,0));
}
return label;
}
};
}
public static void main(String[] args) {
EventList<String> rawList = GlazedLists.eventListOf(
"apple", "appricot", "acorn", "blueberry", "coconut", "chesnut", "grape");
SeparatorList<String> separatorList =
new SeparatorList<String>(rawList, createComparator(), 1, 1000);
JList list = new JList(new EventListModel<String>(separatorList));
list.setCellRenderer(createListCellRenderer());
JScrollPane scrollPane = new JScrollPane(list);
scrollPane.setBorder(null);
JFrame frame = new JFrame();
frame.add(scrollPane, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
a source to share
You can use what Apple calls SourceList. You can see them in action in iTunes and the Mac OS X Finder. This is an elegant solution to the problem you are describing.
An open source cross-platform Java Swing component for this can be found here: http://explodingpixels.wordpress.com/2008/09/08/mac-widgets-for-java/
a source to share
