英文:
Updating a JavaFX property with the same value
问题
我想要再次为DoubleProperty赋予相同的值。或者一个简单的更新也行。不过很遗憾,我还没有找到这样的可能性,如果这种情况真的有可能的话。
以下代码示例说明了我的意图:
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
public class Test {
public static void main(String[] args) {
DoubleProperty p = new SimpleDoubleProperty();
p.addListener((obs_p, old_p, new_p) -> {
System.out.println("在这里,数值为:" + new_p);
});
p.set(1);
p.set(1);
}
}
或者一个类似这样的解决方案:
p.update() ...
总体上,我想要获得以下输出:
在这里,数值为:1.0
在这里,数值为:1.0
但是只有一行输出。
这样的方法真的存在吗?
英文:
I would like to give a DoubleProperty the same value again. Alternatively a simple update would be nice. Unfortunately i haven't found a possibility yet, if this is possible at all.
The following code example illustrates my intentions:
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
public class Test {
public static void main(String[] args) {
DoubleProperty p = new SimpleDoubleProperty();
p.addListener((obs_p, old_p, new_p) -> {
System.out.println("Here, the value: " + new_p);
});
p.set(1);
p.set(1);
}
}
or a solution that looks something like this:
p.update() ...
Overall I would like to receive the following output:
Here, the value: 1.0
Here, the value: 1.0
But only one line is output.
Do such methods even exist?
答案1
得分: 0
你可以扩展SimpleDoubleProperty类并重写set(double)方法:
public class CustomDoubleProperty extends SimpleDoubleProperty {
@Override
public void set(double d) {
super.set(d);
System.out.println("set value: " + d);
}
}
英文:
You can extend SimpleDoubleProperty and override the set(double);
public class CustomDoubleProperty extends SimpleDoubleProperty {
@Override
public void set(double d) {
super.set(d);
System.out.println("set value: " + d);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论