Reputation: 1337
I have a JavaFx Application which plot a chart.
public class Test2 extends Application {
@Override public void start(Stage stage) {
final SwingNode chartSwingNode = new SwingNode();
chartSwingNode.setContent(
new ChartPanel(generatePieChart())
);
stage.setScene(
new Scene(new StackPane(chartSwingNode))
);
stage.show();
}
private JFreeChart generatePieChart() {
DefaultPieDataset dataSet = new DefaultPieDataset();
dataSet.setValue("China", 1344.0);
dataSet.setValue("India", 1241.0);
dataSet.setValue("United States", 310.5);
return ChartFactory.createPieChart(
"Population 2011", dataSet, true, true, false
);
}
public static void main(String[] args) { launch(args); }
}
Then I call it from Swing using a button.
_btnStart.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
new Thread() {
@Override
public void run() {
Application.launch(Test2.class);
}
}.start();
}
});
When I click button, the chart will be shown. Now I want to update the chart. How can I pass value to Test2 class and update the chart? Should I use JFXPanel instead?
Upvotes: 0
Views: 160
Reputation: 735
If you really need to make it in such a way, why not using a Singleton?
Create a class ChartSingleton.java:
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
public class ChartSingleton {
private static ChartSingleton instance = null;
private static ChartPanel chartPanel = null;
private ChartSingleton() {
chartPanel = new ChartPanel(generatePieChart());
}
public static ChartSingleton getInstance() {
if (instance == null) {
instance = new ChartSingleton();
}
return instance;
}
private JFreeChart generatePieChart() {
DefaultPieDataset dataSet = new DefaultPieDataset();
dataSet.setValue("China", 1344.0);
dataSet.setValue("India", 1241.0);
dataSet.setValue("United States", 310.5);
return ChartFactory.createPieChart(
"Population 2011", dataSet, true, true, false
);
}
public ChartPanel getChartPanel() {
return chartPanel;
}
}
Then replace the line:
chartSwingNode.setContent(new ChartPanel(generatePieChart()));
With this:
ChartSingleton chartSingleton = ChartSingleton.getInstance();
chartSwingNode.setContent(chartSingleton.getChartPanel());
Now you can update/modify your chart through the ChartSingleton class using:
chartSingleton.youNewMethodToModifyOrUpdateTheChart();
Upvotes: 1