Multi-leveled (nested?) Jlist in Java

1.7k Views Asked by At

Is there a way to create a JList that has more than one (I'm aiming at three) levels?

Something like this:

level 1 item
  level 2 item
  level 2 item
     level 3 item
level 1 item
level 1 item
  level 2 item
  level 2 item
     level 3 item
     level 3 item

I have (up to)three-level-component GUI in my program, and I would need to somehow enable user to organise the elements of the GUI, to move them above or under each other.

Can it be done with JList, or is there another way of dealing with such things? Maybe some library?

3

There are 3 best solutions below

6
On

You can implement your own ListCellRenderer and your own ListModel.

http://docs.oracle.com/javase/6/docs/api/javax/swing/JList.html

3
On

I think you could, yes, but you're in for a world of hurt that way. JList naturally represents a List from a conceptual point of view, not a tree, meaning most of the ordering logic would have to be done by you. What you're probably interested in is a JTree instead.

0
On

I think you should use JTree for that.