Reputation: 13
I am now work one JavaFX and now I create PieChart I saw in tutorial "http://docs.oracle.com/javafx/2/charts/pie-chart.htm#CIHFDADD" that the pie chart label might be useful, thus I let it show the name and data value as this
private String[] caption = {dataInp.getConstantValue(15, 8)+"\n"+(int)(percent[0]*100)+"%"
, dataInp.getConstantValue(16, 8)+"\n"+(int)(percent[1]*100)+"%"
, dataInp.getConstantValue(17, 8)+"\n"+(int)(percent[2]*100)+"%"
, dataInp.getConstantValue(18, 8)+"\n"+(int)(percent[3]*100)+"%"};
pieChartData = FXCollections.observableArrayList(
new PieChart.Data(caption[0], percent[0]),
new PieChart.Data(caption[1], percent[1]),
new PieChart.Data(caption[2], percent[2]),
new PieChart.Data(caption[3], percent[3]));
chart = new PieChart(pieChartData);
the result is perfectly fine as the initial the next step is make it dynamic following the data user select by
public void setSlice(double percent, int index){
pieChartData.get(index).setPieValue(percent);
pieChartData.get(index).setName(caption[index]+"\n"+(int)(percent*100)+"%");
}
and the line pieChartData.get(index).setName(caption[index]+"\n"+(int)(percent*100)+"%");
cause
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Not on FX application thread; currentThread = AWT-EventQueue-0
Is there anyway to change the slice name? or make the result similar to what I describe above?
Upvotes: 0
Views: 577
Reputation: 36742
The most probable reason that can be seen from the Exception
is that the method setSlice()
is called from a thread other than JavaFX Application Thread
.
There are many ways to resolve this issue:
JavaFX Application thread
You can do something like:
Platform.runLater(new Runnable(){
setSlice(10, 3);
});
or,
public void setSlice(double percent, int index){
Platform.runLater(new Runnable(){
pieChartData.get(index).setPieValue(percent);
pieChartData.get(index).setName(caption[index]+"\n"+(int)(percent*100)+"%");
});
}
Upvotes: 0