I noticed something weird with setting maximum sizes in a boxlayout.
I need a column of panels running down the right side of a window, and I tried to set it up with a box layout. There might be 8-10 or there might be 30-40, so I give them enough space to account for 30-40, but if there's only 8-10 they spread out to take up all the available space and it looks pretty weird. So I figured I'll setMaximumSize for them to stop that from happening...
...and I kept ending up with most of them disappearing.
So I broke it down into a really basic stack of colored panels, trying to see what's going on, and I've figured out how to carry on, but I don't like it.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BoxLayoutDemo extends JFrame {
public BoxLayoutDemo() {
super("JScrollPane Demonstration");
setSize(800, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
init();
setVisible(true);
}
public void init() {
setLayout(null);
JPanel rightPanel = new JPanel();
rightPanel.setBounds(400,0,400,600);
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.PAGE_AXIS));
JPanel greenPanel = new JPanel();
JPanel bluePanel = new JPanel();
JPanel yellowPanel = new JPanel();
greenPanel.setBackground(Color.GREEN);
bluePanel.setBackground(Color.BLUE);
yellowPanel.setBackground(Color.YELLOW);
rightPanel.add(greenPanel);
rightPanel.add(bluePanel);
rightPanel.add(yellowPanel);
add(rightPanel);
}
public static void main(String args[]) {
new BoxLayoutDemo();
}
}
...that gets you just what I'd expect: Three colored boxes taking up the right half of the screen.
If I add something like:
greenPanel.setMaximumSize(new Dimension(400,100));
bluePanel.setMaximumSize(new Dimension(400,100));
yellowPanel.setMaximumSize(new Dimension(400,100));
Then instead the three colored boxes are each only 100px tall and only take up the top half or so of the screen, which is also what I'd expect.
But if I do this...
greenPanel.setMaximumSize(new Dimension(400,100));
yellowPanel.setMaximumSize(new Dimension(400,100));
The blue panel crushes both the other two down to only a couple of px tall.
This probably isn't going to be a problem, I can just set a maximum size for everything, but in a hypothetical situation where that isn't really feasible, is there a way to alter this behavior, or is this just what box layout does?
EDIT:
Okay, so we'll call test1 this:
greenPanel.setMinimumSize(new Dimension(400,100));
yellowPanel.setMinimumSize(new Dimension(400,100));
...which has no effect on anything since the green and yellow panels are about 200 px high anyway.
test2:
greenPanel.setMaximumSize(new Dimension(400,100));
yellowPanel.setMaximumSize(new Dimension(400,100));
...causes the blue panel to crowd out the green and yellow ones as originally described.
test3:
greenPanel.setMinimumSize(new Dimension(400,100));
greenPanel.setMaximumSize(new Dimension(400,100));
yellowPanel.setMinimumSize(new Dimension(400,100));
yellowPanel.setMaximumSize(new Dimension(400,100));
...SHOULD result in green and yellow panels that are 100 px tall, as you described, except that in practice what actually happens is the exact same thing as in test2.