Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4238932
Votes 34
Synopsis a JTextField in gridBagLayout does not properly set MinimumSize
Category java:classes_awt
Reported Against 1.2 , 1.3
Release Fixed
State 6-Fix Understood, bug
Priority: 4-Low
Related Bugs 6299377 , 6467248 , 6611253
Submit Date 17-MAY-1999
Description


/* The following class generates a typical use case for swing. A label and two textfields, which display the length of the requested input.
When there is enough space, everything works fine. But if you narrow the frame so that there is not enough width for the columns of the textfields, both textfields will be crippled to their minimal size although more room is left.
That was not the case in swing 1.0.1 and I think the patch for bug 4118855 is the reason for that.
I think it is correct that JTextfield doesn't return its preferredSize as minimumSize. But it is a bug, that gridBagLayout calculates with minimumSize when there is not enough space for preferredSize.*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class GridBagTest{

  public static void main (String[] args){

    JFrame frame = new JFrame();
    frame.addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent e){
        System.exit(0);
      }
    });

    Container container = frame.getContentPane();
    container.setLayout(new GridBagLayout());
    GridBagConstraints gBC = new GridBagConstraints();

    gBC.gridx      = 0;
    gBC.gridy      = 0;
    gBC.gridwidth  = 1;
    gBC.gridheight = 1;
    gBC.weightx    = 0.0;
    gBC.weighty    = 0.0;
    gBC.fill       = GridBagConstraints.NONE;
    gBC.anchor     = GridBagConstraints.NORTHWEST;
    container.add(new JLabel("Text"), gBC);    

    gBC.gridx      = 1;
    gBC.gridy      = 0;
    gBC.gridwidth  = 1;
    gBC.gridheight = 1;
    gBC.weightx    = 1.0;
    gBC.weighty    = 0.0;
    gBC.fill       = GridBagConstraints.NONE;
    gBC.anchor     = GridBagConstraints.NORTHWEST;
    container.add(new JTextField(16), gBC);    

    gBC.gridx      = 1;
    gBC.gridy      = 1;
    gBC.gridwidth  = 1;
    gBC.gridheight = 1;
    gBC.weightx    = 1.0;
    gBC.weighty    = 0.0;
    gBC.fill       = GridBagConstraints.NONE;
    gBC.anchor     = GridBagConstraints.NORTHWEST;
    container.add(new JTextField(8), gBC);    

    frame.pack();
    frame.setVisible(true);
  }
}

/* Do not tell me to use fill, I wan't the textfields two have different widths.*/
(Review ID: 63228) 
======================================================================




java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

We've got an application which has some custom database connected JTextFields.
The columns property of the JTextFields is set to the columnDisplaySize of the
ResultSetMetaData. The panels primarily use GridBagLayout as the LayoutManager.
Sometimes there isn't enough room for a JTextField to have the size of it's
preferredSize. What the GridBagLayout manager then does is just reduce it's
size to the minimumSize! This is totally unacceptable (minimumSize doesn't
allow for display of a single character!).
What we expect to see is that GridBagLayout gives the available space (smaller
than the preferredSize but greater than the minimumSize) to the JTextField!

There is a bug about this exact problem already on the Bug Parade: 4238932, but
in the evaluation, the ball is first passed to the AWT group and then passed
back to the Swing group. This does not help us (developers) very much.

Please fix this or provide a workaround. The only workaround we currently can
think of is to just manually set the minimumSize of the JTextField's (ugly &
platform dependant).
(Review ID: 112859)
======================================================================
Work Around


Subclass JTextField an override getMinimalSize:

public Dimension getMinimalSize(){
  return (getPreferredSize());
}
======================================================================
Evaluation
The bug is against GridBagLayout, passing this to awt.
  xxxxx@xxxxx   1999-09-16

This is a problem of JTextField not properly setting Minimum and Preferred Size
  xxxxx@xxxxx   2000-07-28

This still appears to be a swing bug.
  xxxxx@xxxxx   2001-08-27

With GridBagLayout if you do not specify a fill other than GridBagConstraints.NONE, the size of the component will be either the min or pref. For example, lets say you have a Component with a min of 10 and a pref of 100. If the parent Container, with a GridBagLayout, only has 20 pixels available, it will adjust the Component to a size of 10 (its pref, 100, is too big). The submitter of this bug is asking for GridBagLayout, if the min/pref doesn't exactly match, that the size by scaled between the min and pref regardless of the fill. As this is a fundamental change in GridBagLayout, I'm reassigning to awt to evaluate if they want to make this change.
  xxxxx@xxxxx   2001-08-27

AWT certainly does not want to make this change in the Merlin timeframe. Downgrading the priority.
  xxxxx@xxxxx   2001-09-05



The problem is still reproducible with tiger b45.
I wonder why this is not RFE.
  xxxxx@xxxxx   2004-04-12
======================================================================

a wrong and high-visible behaviour.
  xxxxx@xxxxx   2005-05-14 10:43:41 GMT

We receieved the attached contribution from java.net user (svenmeier).
After applying the patch, it does look like if fixed the problem.
However, due to a conflict we had to get a new version from Sven.
This new version seems to fix both the BaseLine tests that Scott
added as well as the original bug. Andrei also provided
a regression test for it. The attachment named sven.txt is the
original version, the second one (GridBagLayout.java) is the final 
one after several back and forths to fix various problems.

This problem is now fixed.



  xxxxx@xxxxx   2005-06-15 18:09:12 GMT
Posted Date : 2005-06-15 18:09:12.0

There is a lot of complains for new layouting policy and after huge investigation we found that we may not keep 4238932 fixed and avoid these regressions. For now we back this fix out and probably will introduce new java property for new layouting policy:
(-Djava.awt.gridbaglayout.preferredsizepolicy = true ?)
Posted Date : 2005-09-23 11:25:50.0

Contribution-Forum:https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?messageID=11998&forumID=1463
Posted Date : 2006-03-15 02:46:52.0
Comments
  
  Include a link with my name & email   

Submitted On 16-DEC-1999
jtr
I was just about to enter a bug against GridBagLayout with a similiar comment
when I found this one.
GridBagLayout needs to be changed so it uses some value between the 
minimum size and the preferred size when the preferred size doesn't fit.
Or maybe start with the mininum size and scale by the weights.  



Submitted On 01-AUG-2000
svenmeier
Who did the evaluation on 2000-07-28 ?? That's bullshit, 
the textField isn't to blame for this one. The original 
synopsis was "GridBagLayout cripples new Swing"!!!


Submitted On 28-NOV-2000
svenmeier
You can count the votes on bug #4188906 for this one.


Submitted On 14-MAY-2001
svenmeier
You can count the votes on bug #4247013 for this one too.


Submitted On 14-OCT-2001
bernie01
IMHO: The solution should be to use fill. This could put an
end to the total confusion that GridBagLayout (GBL) creates
(just search the news and you know why). GBL is totally
screwed up.
If minimum, preferred and maximum size AND fill != NONE are
 all set, then GBL should allocate space smoothly between
the extremes depending on allocated space in the cell.
Another fundamental change that we need for this is as follows:
If the available space in the cell is equal or greater than
maximumSize then the component should get maximum size if
corresponding fill is set. If fill is not set then it should
get preferredSize.
Currently GBL totally ignores maximumSize of components in
its cells which is a shame. GBL is such a good tool and it
should be fixed with common sense as to implement what can
be expeced from it.


Submitted On 01-MAR-2002
cpell
If a layout cannot accommodate a preferred size, throwing up
its arms and using the minimum size hardly seems like the
best alternative.  To me, "preferred size" means "I would
like to be this size."  So if I were writing a layout, I
would try to make a child component as close to its
preferred size as possible.  If there isn't enough room,
then make it as large as room permits---in other words, as
close to its preferred size as room permits.  Thus, if the
available space is smaller than the preferred size, I expect
the child component to act like it is set to fill that
dimension, because it is striving for its preferred size.


Submitted On 14-JUL-2004
svenmeier
No comment:

http://madbean.com/blog/2004/17/totallygridbag.html


Submitted On 09-OCT-2004
osgafi
Not only Jtextfields, also Jlabels, and Jscrollpane have this problem


Submitted On 10-APR-2008
gat
According to (6467248) evaluation "When using MToolkit minimum size is set to not-null value" . Is there some reason why SUN's alternate Moo-Tiff toolkit does not replicate this behavour?   Its only 8 years, and still no fix.


Submitted On 28-APR-2008
thetan
There are were several suggestions and all of them has side effects which are critical for developers.



PLEASE NOTE: JDK6 is formerly known as Project Mustang