GridBagLayout - strange resize behaviour
°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
Hi there,
I have done a Swing application using GridBagLayout - looks good,
works fine - but, there is this strange resize behavior.
I have JLabels followed by JTextFields togehter on a JPanel. I reduced
it to 3 rows on the grid.
The Labels are aligned left and don't change their size by resizing,
only the TextFields do.
The problem goes like this:
when I resize the frame horizontally it happens that the space between
the Labels and the Panel, which is 0 pxl, jumps to 1 maybe 2 pxl.
It does not happen every resize, just keep resizing and it will
appear.
thanks for your help
nihil baxter
°°°°°°°°°°°°°°°°°°°°°
import javax.swing.*;
import java.awt.*;
public class GBLTest {
public void init(JPanel panel) {
Dimension defaultDim = new Dimension(120,20);
panel.setLayout(new BorderLayout());
JPanel grid = new JPanel(new GridBagLayout());
GridBagConstraints cons = new GridBagConstraints();
JLabel label;
JTextField textField;
cons.fill = GridBagConstraints.HORIZONTAL;
cons.anchor = GridBagConstraints.NORTH;
//Row 1
label = new JLabel("Label 1");
label.setPreferredSize(defaultDim);
label.setMinimumSize(defaultDim);
cons.gridy = 0;
cons.gridx = 0;
cons.weightx = 0;
grid.add(label,cons);
//--
textField = new JTextField();
textField.setPreferredSize(defaultDim);
textField.setMinimumSize(defaultDim);
cons.gridx = GridBagConstraints.RELATIVE;
cons.weightx = 0.5;
grid.add(textField,cons);
//Row 2
label = new JLabel("Label 2");
label.setPreferredSize(defaultDim);
label.setMinimumSize(defaultDim);
cons.gridy = 1;
cons.gridx = 0;
cons.weightx = 0;
grid.add(label,cons);
//--
textField = new JTextField();
textField.setPreferredSize(defaultDim);
textField.setMinimumSize(defaultDim);
cons.gridx = GridBagConstraints.RELATIVE;
cons.weightx = 0.5;
grid.add(textField,cons);
//--
textField = new JTextField();
textField.setPreferredSize(defaultDim);
textField.setMinimumSize(defaultDim);
cons.gridx = GridBagConstraints.RELATIVE;
grid.add(textField,cons);
//Row 3
label = new JLabel("Label 3");
label.setPreferredSize(defaultDim);
label.setMinimumSize(defaultDim);
cons.gridy = 2;
cons.gridx = 0;
cons.weightx = 0;
grid.add(label,cons);
//--
textField = new JTextField();
textField.setPreferredSize(defaultDim);
textField.setMinimumSize(defaultDim);
cons.gridx = GridBagConstraints.RELATIVE;
cons.weightx = 0.5;
grid.add(textField,cons);
//--
textField = new JTextField();
textField.setPreferredSize(defaultDim);
textField.setMinimumSize(defaultDim);
cons.gridx = GridBagConstraints.RELATIVE;
grid.add(textField,cons);
//--
textField = new JTextField();
textField.setPreferredSize(defaultDim);
textField.setMinimumSize(defaultDim);
cons.gridx = GridBagConstraints.RELATIVE;
grid.add(textField,cons);
panel.add(grid,BorderLayout.NORTH);
}
public static void main(String[] args) {
JFrame frame = new JFrame("GridBagLayoutTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GBLTest gblTest = new GBLTest();
gblTest.init((JPanel)frame.getContentPane());
frame.pack();
frame.show();
}
}
Andrew Thompson - 19 Dec 2003 17:26 GMT
> GridBagLayout - strange resize behaviour
> ????????????????????????????????????????
..I usually do not jmp into qns about gbl, since I
am no expert on them, but I will on this one..
> I have done a Swing application using GridBagLayout - looks good,
> works fine - but, there is this strange resize behavior.
>
> I have JLabels followed by JTextFields togehter on a JPanel. I reduced
> it to 3 rows on the grid.
...
> when I resize the frame horizontally it happens that the space between
> the Labels and the Panel, which is 0 pxl, jumps to 1 maybe 2 pxl.
I compiled and ran your code so's I could get
an easy (ok lazy) picture of what you mean..
Then it struck me.
You have _3_ rows and the layout gives
0,1 or 2 px space.
I bet if you had _4_ rows that space
would become 0, 1, 2 or _3_ px.
The GBL does not add a pixel to any of the
3 in the row until it can add a single pixel to
_each_ of the three, leading to either 0, 1,
or 2 spare.
Adding a border to the various elements may
help make clear if that is the case (always a
good strategy for chasing down fine layout
problems)
HTH
--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site