Issue
I'm trying to figure out how to get and update existing data in a set of dynamically created pie charts in JavaFX. These pie charts are all saved in an ArrayList
. The first task is figure out how to get that data.
As I'm creating these pie charts dynamically from a set of fluid data sources, I cannot make multiple observable lists using code similar to this:
private ObservableList<PieChart.Data> dataList = FXCollections.observableArrayList();
Any ideas on how can I read the data name and value from each dynamically generated pie chart slice?
Solution
Every PieChart
(view) has access to it own ObservableList<PieChart.Data>
(model) via its data
property. Given a List<PieChart>
named list
, you can traverse the list and examine or update each chart's data as warranted:
list.get(0).getData().get(0).setPieValue(42);
for (PieChart p : list) {
for (PieChart.Data data : p.getData()) {
System.out.println(data);
}
}
Console (data from Ensemble8 PieChartApp
):
Data[Sun,42.0]
Data[IBM,12.0]
Data[HP,25.0]
Data[Dell,22.0]
Data[Apple,30.0]
As an alternative to having a list of views, List<PieChart>
, consider maintaining a list of models, List<ObservableList<PieChart.Data>>
.
Answered By - trashgod
Answer Checked By - Marilyn (JavaFixing Volunteer)