Java GUI Toolbar

I am developing a Java Desktop application. In this, I want some toolbars to be at the top of the JFrame (as in regular GUI applications).

I want the user to be able to add / remove toolbars dynamically by clicking on some buttons. How can I implement this (through any layouts or in some other way), so that when the user adds / removes the toolbar, the rest of the space under the toolbar is configured accordingly.

+3
source share
3 answers

I would recommend you use BorderLayout for the program and save the north area for toolbars. In this (northern) area add another container (with BoxLayout or FlowLayout), depending on how you want your toolbars to be added / placed in the container.

Check out this Java Layout Tutorial.

+2
source

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

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

Now, if you later do something like

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

( ), .

+1

, , , , JPanel ( "box" JPanel), JToolBar JPanel JToolBar, . NetBeans, , .

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

0
source

Source: https://habr.com/ru/post/1727468/


All Articles