Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling digit grouping in a JSpinner

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?

like image 573
Kenneth Allen Avatar asked Jun 01 '11 08:06

Kenneth Allen


1 Answers

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);
like image 132
dogbane Avatar answered Sep 23 '22 06:09

dogbane