简体   繁体   中英

How to set JSpinner as non editable?

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?

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.

A bit shorter:

JSpinner spinner = new JSpinner();
spinner.setEditor(new JSpinner.DefaultEditor(spinner));

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM