JSpinner 值更改事件
当jSpinner值改变时如何立即更新。
ChangeListener listener = new ChangeListener() {
public void stateChanged(ChangeEvent e) {
jLabel.setText(e.getSource());
}
};
spinner1.addChangeListener(listener);
上面的代码不会自动更改标签文本,它需要您再次单击任意位置才能更新。
How to make the update immediately when the jSpinner value was changed.
ChangeListener listener = new ChangeListener() {
public void stateChanged(ChangeEvent e) {
jLabel.setText(e.getSource());
}
};
spinner1.addChangeListener(listener);
The code above doesnt change the label text automatically, it required you to click again anyplace to update.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
答案是配置 JFormattedTextField 中使用的格式化程序,它是旋转器编辑器的子级:
不幸的是,掌握它就像介绍性句子一样又长又脏:
一种稍微(但不是很多)更干净的方法可能是子类 NumberEditor 并公开一个允许配置的方法
The answer is to configure the formatter used in the JFormattedTextField which is a child of the spinner's editor:
Unfortunately, getting one's hand on it is as long and dirty as the introductory sentence:
A slightly (but not by much) cleaner way might be to subclass NumberEditor and expose a method which allows the config
您显示的代码看起来是正确的。作为参考,这里有一个工作示例。
附录:当 JSpinner 获得焦点时,左右箭头键会移动插入符号。向上箭头递增,向下箭头递减包含插入符号的字段。旋转器和标签中的变化(实际上)是同时发生的。
访问
JSpinner.DateEditor
,使用父级的getTextField()
方法。然后可以使用合适的插入符侦听器或文本输入侦听器来根据需要更新标签。附录:更新为使用
setCommitsOnValidEdit
,如此处建议。The code you show appears correct. For reference, here is a working example.
Addendum: While the
JSpinner
has focus, the left and right arrow keys move the caret. The up arrow increments and the down arrow decrements the field containing the caret. The change is (effectively) simultaneous in both the spinner and the label.To access the
JFormattedTextField
of theJSpinner.DateEditor
, use the parent'sgetTextField()
method. A suitable caret listener or text input listener may then be used to update the label as desired.Addendum: Update to use
setCommitsOnValidEdit
, as suggested here.这可能是一个迟到的答案,但你可以使用我的方法。
正如上面提到的 spuas 问题是
stateChanged
事件仅在焦点丢失或按下 Enter 键时触发。使用 KeyListener 也不是一个好主意。
最好使用
DocumentListener
来代替。我对 spuas 的示例进行了一些修改,这就是我得到的:It might be an late answer but you may use my approach.
As spuas mentioned above the problem is that
stateChanged
event is fired only when focus is lost or Enter key is pressed.Using KeyListeners is not an good idea as well.
It would be better to use
DocumentListener
instead. I modified spuas's example a bit and that's what I got:这里的问题是,当您通过键盘键入手动编辑
JSpinner
值时,直到JSpinner
失去焦点之前,不会触发stateChanged
事件。 code> 或直到按下 Enter 键。如果您想上传值,则需要一个
KeyListener
,它将在JSpinner
中为每个键入的键执行setValue
。我在这里留下一个带有
SpinnerNumberModel
的JSpinner
示例:Problem here is that when you edit the
JSpinner
value manually by typing from the keyboard, thestateChanged
event is not fired until the focus is lost by theJSpinner
or until Enter key has been pressed.If you want to upload the value, a
KeyListener
is needed which will perform asetValue
in theJSpinner
for each typed key.I leave an example here for a
JSpinner
with aSpinnerNumberModel
:最后一个答案可以稍微重新安排一下,使其更加灵活。您可以简单地使用这个新的 MyJSpinner 来代替任何 JSpinner。最大的变化是您可以将这个新版本与 JSpinner 的任何底层模型(int、double、byte 等)一起使用
The last answer can be rearranged a little to make it a little more flexible. You can simply use this new MyJSpinner in place of any JSpinner. The biggest change is that you can use this new version with any underlying model of the JSpinner (int, double, byte, etc.)
我通过在语句后面添加 requestFocus() 方法解决了类似的情况,如下所示:
希望这会是一个很好的帮助。有好的编码。
I resolved a similar situation by adding the requestFocus() method after the statement, like this:
Hoping this would be a good help. Have nice coding.
我是新人,所以我可能会违反一些规则,并且可能会迟到。但我发现一些答案有点令人困惑,所以我在 NetBeans IDE 中进行了尝试,发现如果右键单击放在 jform 上的 jspinner GUI 组件并转到 events-> ,就会出现这样的情况。更改,将为您生成代码。
I'm new so I might be breaking some rules and I might be late. But I found some of the answers a bit confusing so I played around in NetBeans IDE and found that if you right click on the jspinner GUI component placed on your jform and go to events-> change, code will be generated for you.