JSpinner Value change Events(JSpinner 值变化事件)
问题描述
当 jSpinner 值改变时如何立即更新.
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.
推荐答案
答案是配置JFormattedTextField中使用的格式化程序,它是微调器的编辑器的子项:
The answer is to configure the formatter used in the JFormattedTextField which is a child of the spinner's editor:
formatter.setCommitsOnValidEdit(true);
不幸的是,得到一个人的手就像介绍句一样又长又脏:
Unfortunately, getting one's hand on it is as long and dirty as the introductory sentence:
final JSpinner spinner = new JSpinner();
JComponent comp = spinner.getEditor();
JFormattedTextField field = (JFormattedTextField) comp.getComponent(0);
DefaultFormatter formatter = (DefaultFormatter) field.getFormatter();
formatter.setCommitsOnValidEdit(true);
spinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
LOG.info("value changed: " + spinner.getValue());
}
});
一种稍微(但不是很多)更简洁的方法可能是继承 NumberEditor 并公开一个允许配置的方法
A slightly (but not by much) cleaner way might be to subclass NumberEditor and expose a method which allows the config
这篇关于JSpinner 值变化事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JSpinner 值变化事件
基础教程推荐
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01