tags:

views:

73

answers:

2

I am developing a Java Desktop Application. In that I want some toolbars at the top of the JFrame (as in usual GUI appllications).

I want to allow user to add/remove toolbars dynamically by clicking on some buttons. How can I implement this (through any Layouts or some other way) so that when a user add/removes a toolbar, the rest of the space below the toolbar is adjusted accordingly.

A: 

If you use the correct LayoutManager and add/remove the components, the layout should be computed automatically.

  JPanel p = new JPanel(new BorderLayout());
  p.add(someComponent, BorderLayout.CENTER);

Now if you later execute something like

  SwingUtilities.invokeLater(new Runnable() {
    public void run() {
      p.add(newComponent, BorderLayout.NORTH);
    }
  });

I think (but haven't tested) that the panel will automatically relayout itself to match the new configuration.

perdian
If you're responding to button clicks, you don't even have to go through the hassle of using `invokeLater` since you're already on the EDT.
Carl Smotricz
For a toolbar I would use BorderLayout.PAGE_START instead of NORTH
willcodejavaforfood
+1  A: 

I would recommend you using BorderLayout for the program and keep the North area for the toolbars. To this (North) area add another container (with BoxLayout or FlowLayout), depending on how you want your toolbars to be added/where placed in the container.

Have a look at this Java Layout Manager tutorial.

Artur