1

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).

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
TOM-12
  • 65
  • 1
  • 1
  • 10
  • 2
    Well, you'd better have a JList when you need a list and a JTree when you need a tree, seems pretty logical – Dici Dec 07 '14 at 10:36
  • You might be able to use a custom `ListCellRenderer` and some HTML or `EmptyBorder` to force the spacing, but I agree with Dici, if you have nail, don't use a screw driver ... – MadProgrammer Dec 07 '14 at 10:48

1 Answers1

2

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.
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433