更新具有相同值的JavaFX属性

huangapple go评论95阅读模式
英文:

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);
    }
}

huangapple
  • 本文由 发表于 2020年10月16日 17:43:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64386740.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定