Home > Mobile >  How to set width of specific panel with BoxLayout Manager
How to set width of specific panel with BoxLayout Manager

Time:02-22

The code below places 3 JPanels inside a JFrame. I want the blue colored panel to have a width of 300 (assume the enclosing Frame has a width of greater than 300). The width of the other two panels should be the remainder. How do I do that?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PanelTest extends JFrame { 
    private JPanel leftpanel;

    public PanelTest() {
        getContentPane().setLayout(new BoxLayout(this.getContentPane(), BoxLayout.X_AXIS));
        this.leftpanel = new JPanel() {         
            @Override
            public Dimension getMaximumSize() {
                return new Dimension(300, 9999);
            }
            
            @Override
            public Dimension getMinimumSize() {
                return new Dimension(300, 0);
            }   
        };
        this.leftpanel.setBackground(Color.blue);
        getContentPane().add(leftpanel);

        JPanel rightpanel = new JPanel();
        getContentPane().add(rightpanel);
        rightpanel.setLayout(new BoxLayout(rightpanel, BoxLayout.Y_AXIS));
        
        JPanel upperpanel = new JPanel();
        upperpanel.setBackground(Color.red);
        rightpanel.add(upperpanel);
        
        JPanel lowerpanel = new JPanel();
        lowerpanel.setBackground(Color.yellow);
        rightpanel.add(lowerpanel);
        pack();
        setVisible(true);
        setExtendedState(Frame.MAXIMIZED_BOTH);
    }
    
    public static void main(String[] args) {
        new PanelTest();
    }
}

enter image description here

I slightly updated the above code using @camickr's suggestion of using BorderLayout. The width is now as desired. Thanks @camickr.

I still think BoxLayout should have respected the minimum and maximum sizes. Taking those into consideration, it should have set the width to 500.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PanelTest extends JFrame { 
    public PanelTest() {
        getContentPane().setLayout(new BorderLayout());
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel leftpanel = new JPanel();
        int width = 500;
        leftpanel.setPreferredSize(new Dimension(width, 1));
        leftpanel.setBackground(Color.blue);
        getContentPane().add(leftpanel, BorderLayout.LINE_START);

        JPanel rightpanel = new JPanel();
        getContentPane().add(rightpanel, BorderLayout.CENTER);
        rightpanel.setLayout(new BoxLayout(rightpanel, BoxLayout.Y_AXIS));
        
        JPanel upperpanel = new JPanel();
        upperpanel.setBackground(Color.red);
        rightpanel.add(upperpanel);
        
        JPanel lowerpanel = new JPanel();
        lowerpanel.setBackground(Color.yellow);
        rightpanel.add(lowerpanel);
        pack();
        setVisible(true);
        setExtendedState(Frame.MAXIMIZED_BOTH);
    }
    
    public static void main(String[] args) {
        new PanelTest();
    }
}

CodePudding user response:

The most important size of a component is the preferred size. Most layout managers will use this size first and then maybe use the minimum/maximum sizes depending on the space available.

If you don't specify a preferred size of the panel it will be (10, 10) since this is the default size of a panel using the FlowLayout when no components are added.

This size is outside the bounds of your minimum/maximum values so it appears the BoxLayout will then allocate space to each component in a ratio based on the maximum size of each panel. The blue panel has a size of 300 and the other panel has a size of Integer.MAX_VALUE so much more space gets allocated to the other panel.

One solution is to add:

leftpanel.setPreferredSize(new Dimension(300, 1));

I want the blue colored panel to have a width of 300

I would not change the content pane to use a BoxLayout and instead just use the default BorderLayout of the frame. Then you:

  1. add the blue panel to the BorderLayout.LINE_START.
  2. Add the panel containing the red/yellow components to the BorderLayout.CENTER.

The BorderLayout will respect the width of the blue panel and give the remaining space to the component in the center.

Using this approach there is no need to override the minimum/maximum values of the blue panel.

Of course you would still need to set the preferred size of the blue panel.

  • Related