1

I have a JFrame which sets the content pane to a JPanel, which has a BoxLayout and two more JPanels, one is a "top bar" and the other is the content I want to work with. The top bar should occupy precisely the size I'm giving it and the content panel should take up the rest, but instead they both take half of the space. What am I doing wrong?

My classes:

public class TopBar extends JPanel
{
    public TopBar()
    {
        setLayout(new FlowLayout());
        setPreferredSize(new Dimension((int) (MyFrame.WIDTH / MyFrame.COMPRESSION), MyFrame.TOPBAR));
        add(new JButton("something"));
    }
}


public class ContentPanel extends JPanel
{   
    public ContentPanel()
    {
        setLayout(null);
    }
}

public class MyJpanel extends JPanel
{
    private JPanel topPanel;
    private JPanel contentPanel;

    public MyJpanel()
    {
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        topPanel = new TopBar();
        contentPanel = new ContentPanel();

        add(topPanel);
        add(contentPanel);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Mark Uivari
  • 245
  • 2
  • 5
  • 16
  • For your top bar, I think `JToolBar` uses `BoxLayout` internally. – Catalina Island Dec 28 '16 at 12:09
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson Dec 28 '16 at 14:59

3 Answers3

1

but instead they both take half of the space

A BoxLayout will allocate extra space to each component up to the components maximum size.

The top bar should occupy precisely the size I'm giving it and the content panel should take up the rest,

Then don't use a BoxLayout. The default layout manager for the content pane of the JFrame is a BorderLayout, which does exactly what you want.

Read the section from the Swing tutorial on How to Use BorderLayout for more information and working examples.

camickr
  • 321,443
  • 19
  • 166
  • 288
-1

You should use setSize instead of setPreferredSize.

Andrzej Kasp
  • 177
  • 1
  • 2
  • 8
-1

My error was that I did not set the preferredSize of both panels, if i set the size of the contentPane aswell it'll work.

Mark Uivari
  • 245
  • 2
  • 5
  • 16
  • See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Dec 28 '16 at 14:58
  • Note that it is rarely necessary to explicitly handle the preferred size of component by either setting or overriding the preferred size. If you post an MCVE/SSCCE & ASCII art as suggested in my comments to the question, it will become more clear if this GUI actually needs the rare solution, or more likely, using layouts, suggested component sizes, and white space (in the form of layout padding or empty borders) to enforce a minimum size. – Andrew Thompson Dec 28 '16 at 15:01
  • (1-) each Swing component will determine its own preferred size. It is then the job of the layout manager to use this information to determine the size and location of each component. – camickr Dec 28 '16 at 15:26