我想有这是由两个属性,即currentPrice
和volumeHeld
,其中currentPrice
实际上是通过下载谷歌资助的股票价格,每10秒获得相乘,获得的属性total
。它每10秒自动更新一次。JavaFX的:绑定工作不正常
现在getCurrentPrice()
被初始化为0
,如代码所示。 10秒钟后,它提供了一个新的价值,这一切工作正常。
但是在下面的绑定方法中,当currentPrice
属性发生更改时,total
不会自动更新。
totalBinding = Bindings.createDoubleBinding(() -> {
System.out.println("current price: " + getCurrentPrice() + "vol held: " + getVolumeHeld());
return getCurrentPrice() * getVolumeHeld();
});
total.bind(totalBinding);
问题:我发现上面的createDoubleBinding
语句中的getCurrentPrice()
有值为0(如上所述),当它的值被改变,改变是不是在total
属性传播。我的意思是,即使当前价格已经改变,total
属性也无法从getCurrentPrice()
中提取新值。
所以问题是双重的,但我猜的解决方案,对于这两个问题,我下面会类似,如果不完全相同:
如何解决上述问题?
稍后,我将在
total
属性绑定到另一个属性制定出总的total
财产的所有Trade
对象)。这失败了,它总是等于0. 这个方法写在不同的类中,即不在Trade类中。
UPDATE:如下图所示
代码:
class SummaryofTrade{
...
sumOfTotals = new ReadOnlyDoubleWrapper();
sumOfTotalsBinding = Bindings.createDoubleBinding(() -> {
double sum = 0;
for(Trade t : this.observableListOfTrades){
sum += t.getTotal();
}
return sum;
}, total); // I cannot put "total" as a second parameter, as it is a property that resides in the Trade class , not this class.
sumOfTotals.bind(sumOfTotalsBinding);
...
}
错误日志消息:
Caused by: java.lang.Error: Unresolved compilation problem:
total cannot be resolved to a variable
请注意:sumOfTotalsBinding
和sumOfTotals
住在另一个类。
规范贸易对象如下:
class Trade{
...
private final ReadOnlyDoubleWrapper total;
private final ReadOnlyDoubleWrapper currentPrice;
private DoubleProperty volumeHeld;
public DoubleBinding totalBinding;
private final ScheduledService<Number> priceService = new ScheduledService<Number>() {
@Override
public Task<Number> createTask(){
return new Task<Number>() {
@Override
public Number call() throws InterruptedException, IOException {
return getCurrentPriceFromGoogle();
}
};
}
};
public Trade(){
...
priceService.setPeriod(Duration.seconds(10));
priceService.setOnFailed(e -> priceService.getException().printStackTrace());
this.currentPrice = new ReadOnlyDoubleWrapper(0);
this.currentPrice.bind(priceService.lastValueProperty());
startMonitoring();
this.total = new ReadOnlyDoubleWrapper();
DoubleBinding totalBinding = Bindings.createDoubleBinding(() ->
getCurrentPrice() * getVolumeHeld(),
currentPriceProperty(), volumeHeldProperty());
total.bind(totalBinding);
}
// volume held
public double getVolumeHeld(){
return this.volumeHeld.get();
}
public DoubleProperty volumeHeldProperty(){
return this.volumeHeld;
}
public void setVolumeHeld(double volumeHeld){
this.volumeHeld.set(volumeHeld);
}
// multi-threading
public final void startMonitoring() {
priceService.restart();
}
public final void stopMonitoring() {
priceService.cancel();
}
public ReadOnlyDoubleProperty currentPriceProperty(){
return this.currentPrice.getReadOnlyProperty();
}
public final double getCurrentPrice(){
return currentPriceProperty().get();
}
// total
public final Double getTotal(){
return totalProperty().getValue();
}
public ReadOnlyDoubleProperty totalProperty(){
return this.total;
}
}
UPDATE 2015年9月15日:
我想阐述在这里更合理的方式我的问题。让我知道这是否没有意义。谢谢。
首先,在上述Trade class
(请注意上面的代码已被更新并指定的属性依赖),每个交易对象包含一个total
属性,这是currentPrice
和VolumeHeld
产物。如果用户手动编辑当前价格和持有量的价值。 total
属性将自动更新。
现在,我有一个Trade对象的ObservableList,它们每个都有一个total
属性。我的目标是总结可观察列表中每个Trade对象的total
属性,并将总和绑定到名为sumOfTotals
的变量。这是在一个名为SummaryOfTrade
的课程中完成的。每当可观察列表中任何一个交易的total
属性发生更改时,sumOfTotals
属性也应该自动更改。
class SummaryofTrade{
...
// within constructor, we have
sumOfTotals = new ReadOnlyDoubleWrapper();
sumOfTotalsBinding = Bindings.createDoubleBinding(() -> {
double sum = 0;
for(Trade t : this.observableListOfTrades){
sum += t.getTotal();
}
return sum;
}, totalProperty());
sumOfTotals.bind(sumOfTotalsBinding);
...
}
这就是问题的用武之地。Eclipse是说,它不承认贸易对象的属性,totalProperty
。错误消息如下所示。
错误日志消息:
Caused by: java.lang.Error: Unresolved compilation problem:
The method totalProperty() is undefined for the type SummaryOfTrade
我已经指定的属性依赖已经又Eclipse是抛出一个错误。我应该如何解决这个问题?
为什么从@AlmasB回答不回答这个问题,这不是完全清楚(尤其是哪一类的属性定义中并没有区别在所有你如何编写绑定)。绑定的值保持为零,因为您没有在绑定中指定依赖关系。如果你在答案中解决它,它现在应该可以工作。也许你可以用[MCVE]更新来显示剩下的问题是什么? –
从上面,我确实在'createDoubleBinding'语句中指定了第二个参数'total',即绑定中的依赖关系,但eclipse不能识别它。错误在于'total property'不在'SummaryOfTrade'类中,所以我不能引用它。如果这仍然令人困惑,我会在今晚重新更新它。 – mynameisJEFF
@James_D请参阅更新,让我知道它是否没有意义。谢谢 – mynameisJEFF