Results 1 to 5 of 5
  1. #1
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default JSpinner resizes, I don't want that.

    Greetings,

    Suppose I have a JPanel with a FlowLayout. I populate the JPanel with a few JSpinners. They all have a SpinnerListModel with simple Strings. So far there's no problem. Not all Strings in those models are equal in size, neither in the number of characters, nor in their display length. Suppose the current value of a JSpinner is a short String while its initial value was a longer String. The currently displayed String has a bit of white space on its left side.

    So far so good but after I have hidden the JPanel component (it is part of a modeless JDialog) and show it again the JSpinner has resized itself to display the current (shorter) String so the extra space on the left of the String is gone and the longer (initial) String doesn't fit in the Spinner anymore.

    My question is: how do I make a JSpinner size itself w.r.t. the longest String in its model and make it stay that way?

    kind regards,

    Jos
    Last edited by JosAH; 05-29-2010 at 08:14 PM.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    The preferred size is based on the size of the text field and the size of the buttons. You can fix the size of the text field editor by using:

    Java Code:
    ((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().setColumns(10);

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by camickr View Post
    The preferred size is based on the size of the text field and the size of the buttons. You can fix the size of the text field editor by using:

    Java Code:
    ((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().setColumns(10);
    Thanks a lot, that helps me out; the 'setColumns' measurement isn't very accurate for non-monospaced fonts is it? (I know it's in the API docs so I can't expect more ;-) There's one thing I find a bit strange: if I use a SpinnerNumberModel in the range, say, [0 ... 1000] the JSpinner can display the widest number and doesn't resize itself ...

    Thanks again and

    kind regards,

    Jos

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    There's one thing I find a bit strange: if I use a SpinnerNumberModel in the range, say, [0 ... 1000] the JSpinner can display the widest number and doesn't resize itself ...
    The spinner editor in this case is a JFormattedTextField which has its own preferred size calculation which I assume is based on the mask used, so therefore it can determine the preferred size even on an empty field.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by camickr View Post
    The spinner editor in this case is a JFormattedTextField which has its own preferred size calculation which I assume is based on the mask used, so therefore it can determine the preferred size even on an empty field.
    I noticed, and it does it quite clever, e.g. if I set a SpinnerNumberModel to the range [0, 1111] the maxim value isn't as wide as, say 999 or 888 but the JFormattedTextField is more clever than I ;-)

    I'm a curious puppy so I immediately scrutinzed the sources but (as always) I couldn't find the relevant parts in there; those Swing classes keep me puzzled (also as always).

    kind regards,

    Jos

Similar Threads

  1. [HELP]JSpinner Left Alignment
    By Hugo in forum AWT / Swing
    Replies: 7
    Last Post: 07-25-2011, 08:02 PM
  2. get date, time from jSpinner
    By cowboy in forum New To Java
    Replies: 3
    Last Post: 12-08-2009, 12:32 PM
  3. Jtable Jspinner help
    By chiragkini in forum AWT / Swing
    Replies: 5
    Last Post: 08-15-2009, 04:36 AM
  4. Replies: 0
    Last Post: 07-11-2008, 04:40 PM
  5. Replies: 0
    Last Post: 07-11-2008, 03:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •