-
Bug
-
Resolution: Not an Issue
-
P1
-
None
-
1.1.2
-
sparc
-
solaris_2.5.1
The GridBagLayout doesn't always resized its children appropriately.
Specifically, the problem in NameView is that a child that initially
takes up 1/3 of the window, gets resized much smaller when something
happens to cause the window to be repainted and the layout manager
to resize its children. This happens on Solaris and NT.
To reproduce, compile and run the following program. When it's
running, play around with resizing the window. Stretch it out and
then resize it smaller. The list will sometimes get very skinny and
is definitely not taking up 1/3 of the window.
========================== GbagTest.java ==========================
import java.awt.*;
public class GbagTest extends Frame {
Panel mainPanel;
GbCanvas canvas;
List list;
public GbagTest() {
super("Gridbag Resize Test");
GridBagLayout gbag = new GridBagLayout();
GridBagConstraints c = null;
mainPanel = new Panel();
mainPanel.setLayout(gbag);
canvas = new GbCanvas();
list = new List();
list.setBackground(Color.green);
c = setupConstraints(0, 0, 1, 1, 2.0, 1.0,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(0,0,0,0), 0, 0);
gbag.setConstraints(canvas, c);
mainPanel.add(canvas);
c = setupConstraints(1, 0, 1, 1, 1.0, 1.0,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(0,0,0,0), 0, 0);
gbag.setConstraints(list, c);
mainPanel.add(list);
add("Center", mainPanel);
pack();
}
public static GridBagConstraints setupConstraints(int x, int y,
int width, int height, double weightx, double weighty, int anchor,
int fill, Insets insets, int ipadx, int ipady)
{
GridBagConstraints c = new GridBagConstraints();
c.gridx = x;
c.gridy = y;
c.gridwidth = width;
c.gridheight = height;
c.weightx = weightx;
c.weighty = weighty;
c.anchor = anchor;
c.fill = fill;
c.insets = insets;
c.ipadx = ipadx;
c.ipady = ipady;
return c;
}
public static void main(String args[]) {
GbagTest gt = new GbagTest();
gt.show();
}
}
class GbCanvas extends Canvas {
public Dimension getPreferredSize() {
return (new Dimension(300, 500));
}
public void paint (Graphics g) {
if (g != null) {
setBackground(Color.yellow);
g.drawString("Resize my window larger then smaller, please.", 20, 30);
g.drawString("The scrolling list (green) is supposed to take up 1/3 of the window's width.", 20, 50);
g.drawString("Notice how a horizontal scrollbar appears in the middle of the list.", 20, 70);
}
}
}
Specifically, the problem in NameView is that a child that initially
takes up 1/3 of the window, gets resized much smaller when something
happens to cause the window to be repainted and the layout manager
to resize its children. This happens on Solaris and NT.
To reproduce, compile and run the following program. When it's
running, play around with resizing the window. Stretch it out and
then resize it smaller. The list will sometimes get very skinny and
is definitely not taking up 1/3 of the window.
========================== GbagTest.java ==========================
import java.awt.*;
public class GbagTest extends Frame {
Panel mainPanel;
GbCanvas canvas;
List list;
public GbagTest() {
super("Gridbag Resize Test");
GridBagLayout gbag = new GridBagLayout();
GridBagConstraints c = null;
mainPanel = new Panel();
mainPanel.setLayout(gbag);
canvas = new GbCanvas();
list = new List();
list.setBackground(Color.green);
c = setupConstraints(0, 0, 1, 1, 2.0, 1.0,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(0,0,0,0), 0, 0);
gbag.setConstraints(canvas, c);
mainPanel.add(canvas);
c = setupConstraints(1, 0, 1, 1, 1.0, 1.0,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(0,0,0,0), 0, 0);
gbag.setConstraints(list, c);
mainPanel.add(list);
add("Center", mainPanel);
pack();
}
public static GridBagConstraints setupConstraints(int x, int y,
int width, int height, double weightx, double weighty, int anchor,
int fill, Insets insets, int ipadx, int ipady)
{
GridBagConstraints c = new GridBagConstraints();
c.gridx = x;
c.gridy = y;
c.gridwidth = width;
c.gridheight = height;
c.weightx = weightx;
c.weighty = weighty;
c.anchor = anchor;
c.fill = fill;
c.insets = insets;
c.ipadx = ipadx;
c.ipady = ipady;
return c;
}
public static void main(String args[]) {
GbagTest gt = new GbagTest();
gt.show();
}
}
class GbCanvas extends Canvas {
public Dimension getPreferredSize() {
return (new Dimension(300, 500));
}
public void paint (Graphics g) {
if (g != null) {
setBackground(Color.yellow);
g.drawString("Resize my window larger then smaller, please.", 20, 30);
g.drawString("The scrolling list (green) is supposed to take up 1/3 of the window's width.", 20, 50);
g.drawString("Notice how a horizontal scrollbar appears in the middle of the list.", 20, 70);
}
}
}
- relates to
-
JDK-4150133 GridBagLayout sizes JScrollPane incorrectly
-
- Closed
-