8

I would like to create a list of expandand/collapse panels, like on this image:

expandand collapse panels

I haven't found any swing component for this, so I began to create something like this. I tried to put buttons one under the other, which fill the available width, but doesn't really works. I can only see the last added one.

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());

    panel.add(new JButton("Delphi Projects"), BorderLayout.NORTH);
    panel.add(new JPanel(), BorderLayout.NORTH); // hidden panel
    panel.add(new JButton("Delphi Projects | Delphi Files"), BorderLayout.NORTH);
    panel.add(new JPanel(), BorderLayout.NORTH); // hidden panel
    panel.add(new JButton("Other Files"), BorderLayout.NORTH);
    panel.add(new JPanel(), BorderLayout.NORTH); // hidden panel
    panel.add(new JButton("C++ Builder Projects | C++ Builder Files"), BorderLayout.NORTH);
    panel.add(new JPanel(), BorderLayout.NORTH); // hidden panel

    JScrollPane scroll = new JScrollPane(panel);
Iter Ator
  • 8,226
  • 20
  • 73
  • 164

4 Answers4

12

You can take a look to JXTaskPaneContainer and JXTaskPane from SwingX project which have these advantages:

  • They are components just like a JPanel is so no extra effort is required to work with them.
  • Unlike accordions you can have more than one panel expanded at any time.
  • They have a cool and smooth effect on collapsing/expanding events.
  • Unlike trees/tree tables you don't have to mess with either TreeModel nor TreeTableModel at all. Also trees and tree tables are components intended to show data in a hierarhical form, not to add components into it.

If you don't like task panes and want to implement something on your own then you have JXCollapsiblePane (also available in SwingX API).

Check SwingLabs Demos for a complete set of SwingX components demo.

Screenshot

enter image description here

dic19
  • 17,821
  • 6
  • 40
  • 69
  • It seems the originally referenced sites are no more available. But the project still is at https://github.com/githubcatw/swingx-core – Queeg May 31 '22 at 14:25
3

Look at JXTreeTable from the SwingX project: JXTreeTable in action

You might also look at JXTree, which is similar, and might be better, depending your exact needs.

Get it from here.

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
2

The component you're looking for is an accordion. Have a look at this question: Accordion for Swing?

As to your code - you can only see the last one because you're adding them all to a panel with a BorderLayout, with BorderLayout.North as the layout constraint. Each one removes the previously added panel. Try switching to another layout - e.g. BoxLayout.

Community
  • 1
  • 1
Kalev
  • 1,158
  • 7
  • 17
  • 2
    Accordion is a different component intended to show a single panel at any time. I dont' think this is what OP wants. – dic19 Sep 06 '14 at 18:11
0

you have to put a grid panel in your layout panel. so in the "north" part in your layout panel, just add a grip panel of one column and the number of raw you want