JFreeChart is not updating as I expected it to.
I've got the code below. There are 2 panels, one with an increment button, one with a chart that displays the data.
Every time the +1 button is pressed, it is supposed to update the data; but the chart is not displaying the new data.
Many thanks in advance for any help.
public class GUItest3 extends JFrame {
private JPanel evoTabbedPanel, outputPanel;
float tempCtr;
public GUItest3(){
tempCtr = 0;
JToolBar toolBar = new JToolBar();
toolBar.setFloatable(false);
JButton incrementorButton = new JButton ("+1");
incrementorButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
tempCtr++;}
});
toolBar.add(incrementorButton);
evoTabbedPanel = new JPanel();
evoTabbedPanel.add(toolBar);
MyGraphPanel myGraphPanel = new MyGraphPanel();
outputPanel = new JPanel();
outputPanel.setLayout(new GridBagLayout());
outputPanel.add(myGraphPanel);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Generations", evoTabbedPanel);
tabbedPane.addTab("Output", outputPanel);
getContentPane().add(tabbedPane);
pack();
setVisible(true);
}
class MyGraphPanel extends JPanel {
JFreeChart myChart;
ChartPanel chartPanel;
XYSeriesCollection myDataset;
private MyGraphPanel(){
XYSeries myData = new XYSeries( "My data" );
myData.add(1.0, 1.0);
myData.add(2.0, tempCtr);
myDataset = new XYSeriesCollection();
myDataset.addSeries(myData);
myChart = ChartFactory.createXYLineChart("My chart", "X axis", "Y axis",
myDataset, PlotOrientation.VERTICAL, true, true, false);
chartPanel = new ChartPanel(myChart);
this.add(chartPanel);
}
}
}