9

I needed a widget to select a TCP/UDP port, so I wrote the following:

public static JSpinner makePortSpinner()
{
    final JSpinner spinner = new JSpinner(
            new SpinnerNumberModel( DefaultPort, 1024, 65535, 1 ) );
    spinner.setFont( Monospaced );
    return spinner;
}

...Monospaced and DefaultPort being static constants.

I would like to remove the digit grouping characters from the resulting display. For example, the default of 55024 displays as "55,024", where I would like it to be "55024". I know that straight NumberFormat, as I might use with JFormattedTextField, has a setGroupingUsed(boolean) method for this purpose. Is there anything like this for JSpinner? Should I subclass SpinnerNumberModel?

Kenneth Allen
  • 347
  • 3
  • 7

1 Answers1

17

Set the format of the number editor on your spinner:

spinner.setEditor(new JSpinner.NumberEditor(spinner,"#"));

or to be more explicit:

JSpinner.NumberEditor editor = new JSpinner.NumberEditor(spinner);
editor.getFormat().setGroupingUsed(false);
spinner.setEditor(editor);
dogbane
  • 266,786
  • 75
  • 396
  • 414
  • 1
    The second one _almost_ works: it is initially sized and drawn with grouping, but that goes away if I edit it or force it to redraw. The first seems to work fine, though; thank you very much! – Kenneth Allen Jun 01 '11 at 08:38