javaswingexpand

How to create expandable panels using swing?


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

Solution

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

    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