尝试将多个视图用于一个单独的支持模型实例时,我尝试将多个图表绑定到同一个数据属性。但奇怪的是,只有最后一个绑定图表显示数据,而第一个看起来完全是空的,尽管第一个图表上的getData()确实产生了预期的结果,即使更新了底层属性的内容。 / p>
我写了一个简短的例子来说明我的问题,希望有人可以向我透露我的错误并告诉我如何正确地做到这一点。
package javafx.databinding;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleListProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.PieChart.Data;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class TestDataBinding extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception
{
Property<ObservableList<Data>> sourceData =
new SimpleListProperty<Data>(FXCollections.observableList(new ArrayList<Data>()));
sourceData.getValue().add(new Data("first", 1));
sourceData.getValue().add(new Data("second", 2));
PieChart firstChart = new PieChart();
PieChart secondChart = new PieChart();
firstChart.dataProperty().bind(sourceData);
secondChart.dataProperty().bind(sourceData);
HBox layout = new HBox();
layout.getChildren().addAll(firstChart, secondChart);
Scene scene = new Scene(layout, 500d, 200d);
primaryStage.setScene(scene);
primaryStage.show();
System.out.println(firstChart.getData());
System.out.println(secondChart.getData());
Platform.runLater(new Task<Void>()
{
@Override
protected Void call() throws Exception
{
Thread.sleep(5000);
sourceData.getValue().get(0).setPieValue(5);
sourceData.getValue().get(1).setPieValue(3);
System.out.println(firstChart.getData());
System.out.println(secondChart.getData());
return null;
}
});
}
}
除了记录结果
[Data[first,1.0], Data[second,2.0]]
[Data[first,1.0], Data[second,2.0]]
[Data[first,5.0], Data[second,3.0]]
[Data[first,5.0], Data[second,3.0]]
但图表看起来像this
答案 0 :(得分:0)
JavaFX确实记录了这一点......
PieChart.Data
类是数据和视图的混合。例如,图例在数据类中存储为Text
个节点。如您所知,Node
只能包含在一个Parent
元素中。
以下修改示例正常工作:
@Override
public void start(Stage primaryStage) throws Exception {
IntegerProperty first = new SimpleIntegerProperty(1);
IntegerProperty second = new SimpleIntegerProperty(1);
Data d11 = new Data("first", 0);
d11.pieValueProperty().bind(first);
Data d12 = new Data("first", 0);
d12.pieValueProperty().bind(second);
ObservableList<Data> sourceData1 = FXCollections.observableArrayList(d11,d12);
Data d21 = new Data("first", 0);
d21.pieValueProperty().bind(first);
Data d22 = new Data("first", 0);
d22.pieValueProperty().bind(second);
ObservableList<Data> sourceData2 = FXCollections.observableArrayList(d21,d22);
PieChart firstChart = new PieChart(sourceData1);
PieChart secondChart = new PieChart(sourceData2);
HBox layout = new HBox();
layout.getChildren().addAll(firstChart, secondChart);
Scene scene = new Scene(layout, 500d, 200d);
primaryStage.setScene(scene);
primaryStage.show();
System.out.println(firstChart.getData());
System.out.println(secondChart.getData());
new Thread(new Task<Void>() {
@Override
protected Void call() throws Exception {
Thread.sleep(5000);
first.set(5);
second.set(3);
System.out.println(firstChart.getData());
System.out.println(secondChart.getData());
return null;
}
}).start();
}