如何禁用 JSpinner 的键盘和鼠标输入? [英] How do I disable keyboard and mouse entry for a JSpinner?

查看:35
本文介绍了如何禁用 JSpinner 的键盘和鼠标输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试像这样通过键盘或鼠标使 JSpinner 不可编辑时:

When I try to make a JSpinner un-editable by keyboard or mouse like this:

((DefaultEditor) mySpinner.getEditor()).getTextField().setEditable(false);
mySpinner.setEnabled(false);

它禁用任何键盘输入和粘贴,但我仍然可以单击向上/向下按钮并更改值.

It disables any keyboard entry and pasting, but I can still click the up/down buttons and change the value.

如何禁用向上/向下按钮?

How do I disable the up/down buttons?

推荐答案

如果微调器使用 JSpinner.DefaultEditor 或其子类,则以下代码有效(键盘导航禁用,微调按钮不起作用,但可以选择并复制微调器中显示的值).

If the spinner uses a JSpinner.DefaultEditor or its subclass, then the following code works (keyboard navigation disabled, spinner buttons do not work, yet it is possible to select and copy the value displayed in the spinner).

JSpinner component = ...;

component.setEnabled( false );
if ( component.getEditor() instanceof JSpinner.DefaultEditor ) {
   JSpinner.DefaultEditor editor = ( JSpinner.DefaultEditor ) component.getEditor();
   editor.getTextField().setEnabled( true );
   editor.getTextField().setEditable( false );
}

如果微调器有一个自定义编辑器和 JTextComponent 以外的东西,那么可能仍然可以使用相同的方法(禁用微调器,重新启用微调器编辑器使用的实际组件,将该组件标记为已读-仅使用其 API).

If the spinner has a custom editor with something other then JTextComponent, then it is probably still possible to use the same approach (disable the spinner, re-enable the actual component used by the spinner editor, mark that component as read-only using its API).

这篇关于如何禁用 JSpinner 的键盘和鼠标输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆