SpinnerNumberModel 的 PropertyChangeSupport
我想监听 JSpinner 的 SpinnerNumberModel 值的变化。
我创建一个 PropertyChangeSupport 并将模型放入其中。
我需要 propertyChangeListener,因为它向我显示属性的旧值和新值。
该代码片段不起作用:当我单击 JSpinner 时,propertyChange
方法不打印任何内容。
一个简单的 ChangeListener 只给出新值,但我还需要旧值,我怎样才能得到它?
package de.unikassel.jung;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
public class PropertyChangeTest implements PropertyChangeListener {
public static void main(String[] args) {
new PropertyChangeTest();
}
public PropertyChangeTest() {
JFrame frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int value = 1;
int min = 0;
int max = 10;
int step = 1;
SpinnerNumberModel spinnerModel = new SpinnerNumberModel(value, min, max, step);
PropertyChangeSupport pcs = new PropertyChangeSupport(spinnerModel);
pcs.addPropertyChangeListener("value", this);
JSpinner spinner = new JSpinner(spinnerModel);
frame.getContentPane().add(spinner);
frame.setVisible(true);
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println(evt);
System.out.println(evt.getSource());
}
}
I want to listen to the changes of the value of the SpinnerNumberModel for a JSpinner.
I create a PropertyChangeSupport and put the model into it.
I need the propertyChangeListener, because it shows me the old and new value of the property.
The snippet doesn't work: the propertyChange
method prints nothing, when I click on the JSpinner.
A simple ChangeListener give only the new value, but I need also the old value, how can I get it?
package de.unikassel.jung;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
public class PropertyChangeTest implements PropertyChangeListener {
public static void main(String[] args) {
new PropertyChangeTest();
}
public PropertyChangeTest() {
JFrame frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int value = 1;
int min = 0;
int max = 10;
int step = 1;
SpinnerNumberModel spinnerModel = new SpinnerNumberModel(value, min, max, step);
PropertyChangeSupport pcs = new PropertyChangeSupport(spinnerModel);
pcs.addPropertyChangeListener("value", this);
JSpinner spinner = new JSpinner(spinnerModel);
frame.getContentPane().add(spinner);
frame.setVisible(true);
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println(evt);
System.out.println(evt.getSource());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要听模型,而是听编辑器的
JFormattedTextField
,如下所示。Instead of listening to the model, listen to the editor's
JFormattedTextField
, as suggested below.周一早上......这是不抗拒一些评论的经典时间:-)
@timaschew
@Hovercraft
@trashgod
哈哈 - 你已经猜到我不这样做类似的解决方案:它打破了封装,因为它依赖于实现细节,所以除非完全控制 JSpinner 创建并且绝对确定其编辑器永远不会更改,否则不要这样做
Monday morning ... classical time for not resisting a couple of comments :-)
@timaschew
@Hovercraft
@trashgod
haha - you already guessed that I don't like solution: it breaks encapsulation in that it relies on an implementation detail, so don't except when in complete control of the JSpinner creation and are absolutely sure its editor is never changed
要使 PropertyChangeSupport 工作,您需要调用其
firePropertyChange
方法,但更重要的是,支持对象需要有权访问它正在侦听的属性的 setXXX 方法,并且在该方法中它需要调用 PropertyChangeSupport 的 firePropertyChange 方法。因此,我认为要使您的想法发挥作用,您需要扩展模型的类,为其提供 PropertyChangeSupport 对象,为其提供添加和删除侦听器方法,并确保侦听模型的 setValue 方法中所做的更改,该方法是钥匙。在我的示例中,该方法如下所示:这是我使用 PropertyChangeSupport 的示例模型类:
最后是测试类来测试上述类以查看其是否正常工作:
For a PropertyChangeSupport to work you need to call its
firePropertyChange
method, but more importantly the support object needs to have access to the setXXX method of the property that it is listening to, and in that method it needs to call PropertyChangeSupport's firePropertyChange method. And so I think for your idea to work, you'll need to extend the model's class, give it a PropertyChangeSupport object, give it the add and remove listener methods, and be sure to listen to changes made in the model's setValue method which is key. In my example that method looks like this:Here's my sample model class that uses PropertyChangeSupport:
And finally the test class to test out the above class to see if it is working properly:
http://docs.oracle.com/javase/tutorial/javabeans /writing/properties.html#bound
您必须在设置器中触发ThePropertyChange。
http://docs.oracle.com/javase/tutorial/javabeans/writing/properties.html#bound
You have to fireThePropertyChange in the setters.