How to set JSpinner as non editable?

浪子不回头ぞ 提交于 2019-11-26 22:01:05

问题


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?


回答1:


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.




回答2:


A bit shorter:

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



回答3:


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.



来源:https://stackoverflow.com/questions/2902101/how-to-set-jspinner-as-non-editable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!