Search code examples
javaswingjlistjtree

JList like tree


I've got 1 JList using 4 Models. The list shows model depending on which one user chose from JComboBox. What I currently have is that list shows data for all models like this:

group1
group2
group3
...

What I need is that for 3 models it shows like that (above), but for other one it have to show like this (tree structure):

group1
      group1_0
      group1_1
group2
group3
      group3_0
...

Can anyone help me with this? Can anyone point me to some implementation of this or (what else I found is) should I depending on value from JComboBox 'switch' between JList and JTree? And how would I do that without messing with layout (I am using GridBagLayout).


Solution

  • should I depending on value from JComboBox 'switch' between JList and JTree?

    Yes. Unless you can think of a better way.

    And how would I do that without messing with layout(I am using GridBagLayout)

    Put a panel where the list is now. Give the panel a CardLayout1. Add the JTree and JList to the panel and flip between them as needed.

    1. E.G. as shown in this answer.