1

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);
        }
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045

1 Answers1

0

Your ActionListener updates tempCtr, but you need to update the chart's dataset to update the chart:

@Override
public void actionPerformed(ActionEvent e) {
    XYSeries mySeries = myGraphPanel.myDataset.getSeries(0);
    mySeries.addOrUpdate(tempCtr, Math.pow(2, tempCtr));
    tempCtr++;
}

The complete example below illustrates the result after clicking the button twice; it focuses on the essential problem and illustrates a few additional points:

  • Construct and manipulate Swing GUI objects only on the event dispatch thread.

  • Override getPreferredSize() on the enclosing JPanel, as shown here, to establish the enclosing panel's initial size.

  • Use GridLayout, or GridBagLayout with default constraints, to allow the chart to adapt as the enclosing container is resized.

image

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class GUItest3 {

    float tempCtr;

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MyGraphPanel myGraphPanel = new MyGraphPanel();
        f.add(myGraphPanel, BorderLayout.CENTER);
        f.add(new JButton(new AbstractAction("+1") {
            @Override
            public void actionPerformed(ActionEvent e) {
                XYSeries mySeries = myGraphPanel.myDataset.getSeries(0);
                mySeries.addOrUpdate(tempCtr, Math.pow(2, tempCtr));
                tempCtr++;
            }
        }), BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    class MyGraphPanel extends JPanel {

        JFreeChart myChart;
        ChartPanel chartPanel;
        XYSeriesCollection myDataset;

        private MyGraphPanel() {
            setLayout(new GridLayout());
            XYSeries myData = new XYSeries("My data");
            myData.add(tempCtr, tempCtr++);
            myData.add(tempCtr, 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);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(512, 256);
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new GUItest3()::display);
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks, that's really helpful. You said about using the event dispatch thread, was mine not?! If not, please could you explain why not? –  May 24 '19 at 16:05
  • Your example lacks a `main()`, so I mention it for reference; as long as the `GUItest3` constructor runs on the EDT, you're probably OK; I assumed `extends JFrame` was just for illustration, and I just noticed I forgot to elide it after editing; updated. – trashgod May 24 '19 at 17:04