views:

365

answers:

4

I have a list of items in a JList for the user to select. Since it's a lot of items (say, cities in states), I want to divide the list into sections. The section headings should not be selectable, though. So for my cities/states example, this might look like this:

  • State 1
    • City 1
    • City 2
    • City 3
  • State 2
    • City 4
    • City 5
    • City 6

It wouldn't be so difficult to write this myself by embedding JLists in a custom ListCellRenderer, but I'm wondering if there already is a class like that out there.

+3  A: 

Maybe by using JTree ? What you describe is a Tree with two levels.

Valentin Rocher
A tree would be over-complicating the UI. There are far too many trees out there.
Tom Hawtin - tackline
Tom is right, a tree would perhaps work, but it's the wrong metaphor. My illustration in the question wasn't very good, but banjolity's screenshot shows what I mean.
Robert Kosara
+3  A: 
banjollity
That's exactly it! Thanks! I had hoped for something open source (I forgot to say that this is for use in an open source project), but it looks like they have a good policy towards use in open source projects. If somebody can suggest an open source version, I would still appreciate that, though.
Robert Kosara
A: 

You could use what Apple calls a SourceList. You see them in action in iTunes and in Mac OS X's Finder. It is an elegant solution to the problem you describe.

A cross-platform, open source Java Swing component for doing this is here: http://explodingpixels.wordpress.com/2008/09/08/mac-widgets-for-java/

Steve McLeod
That's not really what I was looking for with my question here - but I have been looking for this kind of thing in Java for a while. Great stuff, thank you!
Robert Kosara
+1  A: 
Ken
Thanks, that looks really interesting. I ended up hacking up my own sectioned list in the end, because I didn't feel like including a large library just to get this one component. It works, but it's not perfect. GlazedLists looks like it might fit the bill really well.
Robert Kosara