How to set JSpinner as non editable?
Asked Answered
C

3

18

I am creating time picker using a JSpinner. The text inside the JSpinner is editable. But I want to set the JSpinner as non editable, because there is the chance to give an invalid value. Can anyone help me?

Continual answered 25/5, 2010 at 4:54 Comment(1)
Can you comment on jfpoilpret's answer if it helped or not? And if it did, accept it as the correct one?Mezuzah
G
31

Try the following:

JSpinner spinner = ...;
((DefaultEditor) spinner.getEditor()).getTextField().setEditable(false);

This should work as long as you didn't change the spinner editor yourself by calling spinner.setEditor(...).

Tell us if this helps.

Gazebo answered 25/5, 2010 at 5:37 Comment(3)
I modified your code,It works Fine. JSpinner ampm=new JSpinner(); JFormattedTextField ampmspin=((JSpinner.DefaultEditor)ampm.getEditor()).getTextField(); ampmspin.setEditable(false); Thank you very much.........Continual
I had to add this after I set the spinner's list model. Then it worked.Giffie
When I try this, the spinner can still be edited by click the arrow!Oklahoma
C
13

A bit shorter:

JSpinner spinner = new JSpinner();
spinner.setEditor(new JSpinner.DefaultEditor(spinner));
Crossquestion answered 18/10, 2012 at 15:42 Comment(0)
K
0

When I try this, the spinner can still be edited by click the arrow! – yelliver

You can try setting the step to 0:

mySpinner.setModel(new SpinnerNumberModel(yourDefaultDisplayValue, minValue, maxValue, step));

You can explore the other spinner models and do the same trick I guess.

Kadiyevka answered 11/10, 2018 at 7:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.