[my SSCCE counter-example omitted]
Hi again,
I put together a little demo that shows some of the strange behavior,
although not exactly what I described in the previous post. You see
that I expand the frame, the the (red) JPanel grows to a point and the
decides to go back to it's "packed" size. What is it that I do that
could cause that?
I've no idea, BoxLayout has always worked in a predicable way for me.
I've seen GridBagLayout do similar things (google for totallygridbag).
I would not be able to post a SSCCE example,
everything is too intertwined.
Well either untangle it (which would be sound software engineering and a
good thing to do anyway) or create an SSCCE from scratch.
I suspect the intertwining is at the root of your problem. Remove it!
Well good, whilst that video demonstrates a strange visual effect, I
find it to be of no use in discovering exactly what in your source code
is causing that effect.
You seem to imagine that BoxLayout contains a well known bug that causes
that effect and that everyone else will say "aha that's the xxx bug,
here's a workaround." That is not the case.
An SSCCE is the best way forward. I suggest you make a copy of the
project and delete ruthlessly until you have less than 120 lines of code
that demonstrate the problem.
The following SSCCE reproduces your layout pretty exactly but does
not show the undesired effect shown in your video.
--------------------------------8<-----------------------------------
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
/**
* @author RedGrittyBrick
*/
public class BoxLayoutTest {
public static void main(String[] args) {
final BoxLayoutTest boxTest = new BoxLayoutTest();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
boxTest.createAndShowGUI();
}
});
}
void createAndShowGUI() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder("Addresses"));
panel.setBackground(Color.RED);
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(new PairPanel());
panel.add(new PairPanel());
JFrame f = new JFrame("BoxTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(panel);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
class PairPanel extends JPanel {
PairPanel() {
setBackground(PALE);
setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
add(new AddressPanel());
add(Box.createHorizontalStrut(8));
add(new AddressPanel());
add(Box.createHorizontalGlue());
}
}
class AddressPanel extends JPanel {
AddressPanel() {
setBorder(new CompoundBorder(new EmptyBorder(3, 3, 3, 3), //
new LineBorder(Color.GRAY)));
setBackground(PALE);
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.insets = new Insets(1, 3, 0, 3);
gc.fill = GridBagConstraints.HORIZONTAL;
gc.anchor = GridBagConstraints.LINE_START;
gc.gridwidth = 1;
JTextField category = new JTextField(8);
category.setMinimumSize(category.getPreferredSize());
category.setBackground(Color.BLUE);
add(category, gc);
gc.gridwidth = GridBagConstraints.REMAINDER;
add(new JCheckBox("Primary"), gc);
add(new JTextField(16), gc);
add(new JTextField(16), gc);
gc.insets = new Insets(1, 3, 3, 0);
gc.gridwidth = 2;
add(new JTextField(10), gc);
gc.insets = new Insets(1, 0, 3, 0);
gc.gridwidth = 1;
add(new JLabel(", "), gc);
add(new JTextField(2), gc);
add(Box.createHorizontalStrut(10), gc);
gc.insets = new Insets(1, 3, 3, 3);
gc.gridwidth = GridBagConstraints.REMAINDER;
add(new JTextField(4), gc);
setMaximumSize(getPreferredSize()); // prevent growth
setMinimumSize(getPreferredSize()); // prevent shrink
}
}
static final Color PALE = new Color(232, 232, 232);
}
--------------------------------8<-----------------------------------
Note: I had to work hard to make stretching the frame add dead-space to
the right (as in your video) rather than adding to the length of
JTextFields or otherwise producing a more pleasing visual behaviour.
Nevertheless, it does exactly what you seem to want it to do, without "bug".