RahlSK RahlSK - 1 month ago 17
Java Question

jspinner number editor display

JFrame frame=new JFrame();
frame.setSize(500, 500);
JPanel panel =new JPanel();
panel.setSize(200, 200);
frame.add(panel);


JSpinner m_numberSpinner=new JSpinner();
m_numberSpinner.setSize(100, 100);
SpinnerNumberModel l_spinnerModel = new SpinnerNumberModel(0, 0, 999, 1);
m_numberSpinner = new JSpinner(l_spinnerModel);
JSpinner.NumberEditor l_editor = new JSpinner.NumberEditor(m_numberSpinner);
m_numberSpinner.setEditor(l_editor);

NumberFormat m_numberFormatter = l_editor.getFormat();
m_numberFormatter.setMaximumIntegerDigits(2);
m_numberFormatter.setMinimumIntegerDigits(m_numberFormatter.getMaximumIntegerDigits());
panel.add(m_numberSpinner);
frame.setVisible(true);


Hello, this is a simple java JFrame to show a number spinner.

The first number is shown as '00'. I want it be show as 0. is it possible?
The next number is shown as '01'. I want to be shown as 1. is it possible?

Answer

You specified the number appearence of your JSpinner yourself with NumberFormat to use maximal two numbers

m_numberFormatter.setMaximumIntegerDigits(2);

and for minimum the already set maximum value which is two

m_numberFormatter.setMinimumIntegerDigits(m_numberFormatter.getMaximumIntegerDigits());

That results in always using two digits. So 1 is 01 and so on.

Maybe this is what you want:

m_numberFormatter.setMaximumIntegerDigits(2);
m_numberFormatter.setMinimumIntegerDigits(1);
Comments