开发者

Java GUI Toolbar [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I am developin开发者_C百科g 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.


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


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. I'm using NetBeans, which generates the code, so I can't effectively put the code here.

With that setup, I can use the setVisible() method to show and hide individual buttons and JToolBar's. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜