'How to set width of specific panel with BoxLayout Manager
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();
}
}
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();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

