Search code examples
javaswinglayout-managergridbaglayoutborder-layout

Panel for JToolBar with BorderLayout and its height on GridBagLayout JFrame


I have a JFrame with GridBagLayout. On this frame I have JToolBar and JPanel. The problem appeared when I dropped tool bar back. In an answer to JToolBar IllegalArgumentException when dropped back into GridBagLayout it says that the container for tool bar must be with BorderLayout. Ok, I thought and added one more panel (with BorderLayout) to frame and tool bar to panel. So I have

[JFrame [JPanel1[JToolBar]] [JPanel2]]

Everything works. But now I have a problem - I can't set JPanel1 height so it to be equal to tool bar height and to disappear and appear. I only have weighty as I see, but it's coefficient.

How to solve it?


Solution

  • The default LayOut of a JFrame is BorderLayout, You just add (set) the JToolBar to JFrame as BorderLayout.NORTH and add JPanel as BorderLayout.CENTER. Set JPanel layout as GridBagLayout and do what you want with it.

    I think this is you are looking for.