2

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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Ivan Karlovic
  • 221
  • 5
  • 14

3 Answers3

8

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.

Neil
  • 5,762
  • 24
  • 36
  • 1
    Also, [Java trail for JTree](http://docs.oracle.com/javase/tutorial/uiswing/components/tree.html) – Brian Sep 13 '12 at 15:03
  • I will be able to change indexes of various components and then, on press of a button, apply changes to GUI, all with proper code of course? – Ivan Karlovic Sep 13 '12 at 16:45
  • If by index you mean vertical position in the tree, then you can take an existing tree and convert an index to a position within the tree (and vice versa), though I still think this could easily be quicker than reinventing the JList. At least this way, you're guaranteed that a user cannot create an invalid state assuming all that is required is that it follows a tree structure. – Neil Sep 14 '12 at 07:12
6

I think you should use JTree for that.

brimborium
  • 9,362
  • 9
  • 48
  • 76
2

You can implement your own ListCellRenderer and your own ListModel.

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

km1
  • 2,383
  • 1
  • 22
  • 27
  • 1
    -1, there's no need to reinvent the wheel here...use the appropriate Swing component instead. – user1329572 Sep 13 '12 at 15:02
  • 2
    @user1329572 That depends what the other requirements are for OP. Maybe a JList is the best solution for him (most probably not, but who knows). – brimborium Sep 13 '12 at 15:06
  • @user1329572, why downvote me? If your worried he will reinvent the wheel then downvote his question. Or provide an anser. – km1 Sep 13 '12 at 15:11
  • 1
    @km1 Hiw downvote isn't that wrong. You are showing OP a way that is most probably a very bad. There might be some (rare) occasions where it is the way to go (that's why I didn't downvote). – brimborium Sep 13 '12 at 15:15
  • @km1, It's not his question that's the problem, it's your answer. Yes, you may be able to implement such behavior using a `JList`, but why do that when there already exists a Swing component that is meant to be used in such a manner? And given the fact that you provided no background information or suggestions on how else to approach this problem, there's nothing worthy here of a +1, although the same cannot be said of a -1. – user1329572 Sep 13 '12 at 15:16
  • 2
    Here's a related, two-tier [`ExtendedComboBox`](http://stackoverflow.com/a/5231915/230513). – trashgod Sep 13 '12 at 15:47