<Swing Dev> Review Request for Bug 8146320 JTextField ignores setPreferredSize when having columns

Alexander Scherbatiy alexandr.scherbatiy at oracle.com
Fri Jan 22 10:59:55 UTC 2016


On 21/01/16 15:19, Andrej Golovnin wrote:
> Hi Perm,
>
> I'm sorry, my mistake. To see the difference you must run the example
> with JDK 8, e.g. without your patch. The example contains two fields
> "jdk8" and "jdk9". The "jdk9" field simulates the behaviour of
> JTextField with your patch. Both fields "jdk8" and "jdk9" should have
> the same size on the screen. But after your patch the "jdk9" field is
> only 10 pixel wide.
>
> Suppose developers had following use case in the past:
>
> We need a field which should be 10 columns wide and 20 pixels high.
> The field must be inside of a panel with BoxLayout and laid out
> horizontally from left to right.
>
> What did they done in such situations:
>
> JTextField field = new JTextField(10):
> field.setPreferredSize(new Dimension(10, 20)); // The real width will
> be calculated by JTextField using the number of columns.
>
> JPanel content = new JPanel();
> BoxLayout layout = new BoxLayout(content, BoxLayout.X_AXIS);
> content.setLayout(layout);
>
> content.add(field);
>
> This code implements the use case above (from JavaDocs of BoxLayout:
> BoxLayout attempts to arrange components at their preferred widths
> (for horizontal layout) or heights (for vertical layout).). But your
> change would break now this implementation. After your change the
> field would have only the size of 10 pixels x 20 pixels ant not the
> expected 10 columns x 20 pixels.
   Usually you are right that if there is a public javadoc and 
developers relies on it, changing the API could break their programs.

  This case looks slightly different.  The javadoc for 
JTextField.getPreferredSize() contradicts to the parent 
JComponent.getPreferredSize() documentation: If the preferredSize has 
been set to a non-null value just returns it.

  The JTextField behavior looks like a bug and misleads developers that 
rely on it as on an ordinary JComponent component and expect that a 
custom preferred size should be returned.

  However, this really looks like a corner case between a real bug and 
already used public API.
  It would be better to have more opinions about it.

  Thanks,
  Alexandr.
>
> Best regards,
> Andrej Golovnin




More information about the swing-dev mailing list