user interface – Java GUI Toolbar

user interface – Java GUI Toolbar

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.

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 havent tested) that the panel will automatically relayout itself to match the new configuration.

user interface – Java GUI Toolbar

I got my toolbar to finally work by making a JPanel for all the toolbars (setting the box layout for that JPanel), putting my JToolBar toolbars on the JPanel, and putting my buttons on their respective JToolBar to group them. Im using NetBeans, which generates the code, so I cant effectively put the code here.

With that setup, I can use the setVisible() method to show and hide individual buttons and JToolBars. The documentation states that the validate() method should be used to reposition the buttons, but it works for me without it, at least under Windows 7. When I tried it under Ubuntu 10, the layout of the toolbar was wrong, so my answer is partially correct for now.

Leave a Reply

Your email address will not be published. Required fields are marked *