Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6251892

JSpinner getPreferredSize incorrect

    XMLWordPrintable

Details

    • Cause Known
    • x86
    • windows_2000

    Description

      FULL PRODUCT VERSION :
      java version "1.4.2_02"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_02-b03)
      Java HotSpot(TM) Client VM (build 1.4.2_02-b03, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Windows 2000 version 5.0 build 2195
      I have observed the same problem with Windows XP. Haven't tested other platforms.

      A DESCRIPTION OF THE PROBLEM :
      getPreferredSize on a JSpinner returns a size that reflects the space required for the initial selection, rather than the largest selection. Compare, for example, to JList and JComboBox, which apparently calculate preferred size based on longest value in the list. (Admittedly it may be impractical to determine the "longest value" for an arbitrary spinner model, but it is certainly possible for the canned ones. If nothing else the model could have a function to support this.)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      It's most obvious with the SpinnerListModel. Create a list with entries of significantly different lengths. Create a JSpinner using this model. Add it to a container using a layout manager and in a position where it will get no more than its preferred size.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The JSpinner sizes to fit the longest entry in the list.
      ACTUAL -
      The JSpinner sizes to fit the initially-selected entry in the list.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.swing.*;
      import java.awt.*;

      public class Dork2 extends JFrame
      {
      public static void main(String[] args)
      {
      new Dork2().show();
      }

      public Dork2()
      {
      super("Dork2");
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      Container content=getContentPane();

      String[] list={"a","bb","ccc","dddd"};
      JSpinner spinner=new JSpinner(new SpinnerListModel(list));
      content.add(spinner,BorderLayout.EAST);

      pack();
      setLocationRelativeTo(null);
      }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      In practice I solved it by using a JList instead of a JSpinner. I'm sure one could write code to loop through all the possible values, get font metrics and do stringWidth's on them, pick the longest, and then manually size the control, or subtype it and override getPreferredSize. I'd have to look into how the control is laid out: I'd hate to have to know too much about internals.
      ###@###.### 2005-04-07 10:16:20 GMT

      Attachments

        Activity

          People

            peterz Peter Zhelezniakov
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Imported:
              Indexed: