java尝试在通知中变异 [英] java attempt to mutate in notification
问题描述
我有两个swing ui表单和一个他们都看过的模块。
每个ui都会添加一个监听器来更改属性
并在发生更改时更新自己的文本字段。
I have two swing ui forms and a module that they both look at.
Each ui is adding a listener to the change of an attribute
and update its own textfield when a change occurs.
basiccaly - 他们都应该更新模块并从中进行更新。
有没有一种简单的方法可以在没有绑定框架的情况下做到这一点
basiccaly - they both should update the module and be update from it. Is there a way simple to do it whithout a binding framework
我是这样做的(但我不断尝试在通知中进行变异) -
Here is how I do it (but I keep getting attempt to mutate in notification ) -
关于我的textField的更新
On the update of my textField
myTextField.getDocument().addDocumentListener(new TFDocumentListener() {
protected void userChangedTF() {
Float value = myTextField.getValue();
if (value != null) {
myObj.setMyAttribute(value);
}
}
});
仍然在ui中 - 注册更改
still in the ui - registering the change
myObj.addMyAttributeChangedListener(new ValueChangeListener<Float>() {
@Override public void valueChanged(Float value) {
if (!myTextField.isFocusOwner()) {
myTextField.setValueIn(value);
}
}
});
- 当发生setMyAttribute时 - 它调用此函数
in the module - when setMyAttribute occurs - it calls this function
private void notifyIntervalChanged(float newValue) {
for (ValueChangeListener valueChangeListener : intervalChangedListenersList) {
valueChangeListener.valueChanged(newValue);
}
}
我宣布
public interface ValueChangeListener<T> {
void valueChanged(T Value)
}
推荐答案
如果需要在侦听器中更改相同 JTextComponent
的内容,请在 SwingUtilities中包装更改(例如setText())。 invokeLater()
If you need to change content of the same JTextComponent
in the listener wrap the change (e.g. setText()) in the SwingUtilities.invokeLater()
这篇关于java尝试在通知中变异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!