java 如何将 JSpinner 设置为不可编辑?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2902101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set JSpinner as non editable?
提问by Arivu2020
I am creating time picker using a JSpinner. The text inside the JSpinneris editable. But I want to set the JSpinneras non editable, because there is the chance to give an invalid value. Can anyone help me?
我正在使用JSpinner. 里面的文字JSpinner是可编辑的。但我想将其设置JSpinner为不可编辑,因为有机会给出无效值。谁能帮我?
回答by jfpoilpret
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(...).
只要您没有通过调用spinner.setEditor(...).
Tell us if this helps.
告诉我们这是否有帮助。
回答by Adam Stelmaszczyk
A bit shorter:
短一点:
JSpinner spinner = new JSpinner();
spinner.setEditor(new JSpinner.DefaultEditor(spinner));
回答by Атанас Шалаверов
When I try this, the spinner can still be edited by click the arrow! – yelliver
当我尝试此操作时,仍然可以通过单击箭头来编辑微调器!– 耶利弗
You can try setting the step to 0:
您可以尝试将步长设置为 0:
mySpinner.setModel(new SpinnerNumberModel(yourDefaultDisplayValue,
minValue, maxValue, step));
mySpinner.setModel(new SpinnerNumberModel(yourDefaultDisplayValue,
minValue, maxValue, step));
You can explore the other spinner models and do the same trick I guess.
您可以探索其他微调模型并执行我猜的相同技巧。

