如何将JavaFX progressBar绑定到存储在对象中的双精度值 [英] How to bind a JavaFX progressBar to a double value stored in an Object

查看:64
本文介绍了如何将JavaFX progressBar绑定到存储在对象中的双精度值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在学习,希望您能帮助我

I'm still learning and I hope you can help me:

我有一个对象myObject,该对象存储一个双精度值,该值表示计算的进度百分比 (将myObject初始化后,其值为0D,并经常在myObject自身内部进行更新,直到达到100D).

I have an Object myObject that stores a double value, which represents the percentage progress of a calculation (When myObject is inizialized, the value is 0D, and often updated inside myObject itself until it reaches 100D).

我想将此双重值 bind 绑定到 javaFX progressBar ,以便在更新存储在myObject中的值时,progressBar也将如此.

I'd like to bind this double value to a javaFX progressBar, so that when the value stored in myObject is updated, so the progressBar will.

我听说了有关观察者/观察者/听者的一些事情.
这是我要找的东西吗?有人可以给我看一些代码以便我理解吗?

I heard something about Observer/Observable/Listener.
Is this what I am looking for? Can someone show me some code so that I can understand?

谢谢!

推荐答案

我知道我要迟到五个月,但我只是

I know that I am five months late, but I just had a very similar problem, and during my searching came across your question that nobody else answered. Nobody answered mine either, so I kept digging until I figured it out myself. I assume that if I stumbled over your post looking for an answer, someone else eventually would too, so I wanted to share my answer.

是的,您要查找的是 ObservableDouble .假设您可以将myObject更改为double:

Yes, what you are looking for is an ObservableDouble. Assuming that you are able to change myObject to be a double:

DoubleProperty barUpdater = new SimpleDoubleProperty(myObject);

然后,在初始化的某个位置(或您使用栏的任何地方),绑定您的栏:

And, somewhere in your initialize (or wherever you use the bar), bind your bar to it:

progressBar.progressProperty().bind(barUpdater);

然后,当您希望更新ProgressBar时,可以将更新程序的值设置为.

Then, when you wish to update your ProgressBar, you can set the value of the updater to something else.

barUpdater.set(myObject);

这样做,您将更新ProgressBar正在侦听的ObservableDouble,从而使其更新其值.

In doing so, you will update the ObservableDouble that your ProgressBar is listening for, causing it to update it's value.

有关需要使用的代码示例,请参见在这里我的答案.

See my answer here for an example of the code in use if you need it.

这篇关于如何将JavaFX progressBar绑定到存储在对象中的双精度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆