3

I am facing rendering problem when I call paintComponent() every 500 millisecond to show updated charts. I have around 30 barcharts created by using JFreeChart on Panel.

Rendering with error and How can I solve this problem?

private void ShowGraphs() {

   FirstChart.removeAll();
   SecondChart.removeAll();
   ThirdChart.removeAll();
   FirstChart.add(Label1);
   SecondChart.add(Label2);
   ThirdChart.add(Label3);

   ChartUpdate(P1,FirstChart);
   ChartUpdate(P2,SecondChart);
   ChartUpdate(P3,ThirdChart);
   //FirstChart, SecondChart, ThirdChart is JPanels
   //Tabb is JTabbedPane
   paintComponents(Tabb.getGraphics());
}

This code is called every 500 milliseconds and ChartUpdate(MyObject, Panel) is chart building function on Panel using MyObject's info.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • what do you mean by flickering? – Javant Jul 21 '16 at 16:47
  • The JTabbedPane(doubleBuffered) is repainted using PaintComponent() which causes flickering (I found out recently). Also when it repaint. It repaints with error. How Can I repaint without mistake. – Anhbayar Otgonbayar Jul 21 '16 at 16:54
  • 1
    A relevant question: how can we help identify problems contributing to your issue without pertinent code? Please create and post your [mcve] or [sscce](http://sscce.org). – Hovercraft Full Of Eels Jul 21 '16 at 16:59
  • 1
    `paintComponents(Tabb.getGraphics());` This is not how to do custom painting! See [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/) for details. General tips: 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson Jul 21 '16 at 18:03
  • 1
    `FirstChart.removeAll(); .. FirstChart.add(Label1);` It is likely there is a better approach to this than removing & adding components. Will know what that approach is once you've posted the MCVE/SSCCE. – Andrew Thompson Jul 21 '16 at 18:04
  • Thank you all for the brilliant answers.I got all the changes. Love you all. – Anhbayar Otgonbayar Jul 22 '16 at 13:21

1 Answers1

4

Don't replace the view component. Instead, update the corresponding model and the listening view will update itself in response. In the example below, each ChartPanel returned by createPane() has a Swing Timer that updates its XYSeries every 500 ms.

image

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
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;

/**
 * @see http://stackoverflow.com/a/38512314/230513
 * @see http://stackoverflow.com/a/15715096/230513
 * @see http://stackoverflow.com/a/11949899/230513
 */
public class Test {

    private static final int N = 128;
    private static final Random random = new Random();
    private int n = 1;

    private void display() {
        JFrame f = new JFrame("TabChart");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel p = new JPanel(new GridLayout(0, 1));
        for (int i = 0; i < 3; i++) {
            p.add(createPane());
        }
        f.add(p, BorderLayout.CENTER);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private ChartPanel createPane() {
        final XYSeries series = new XYSeries("Data");
        for (int i = 0; i < random.nextInt(N) + N / 2; i++) {
            series.add(i, random.nextGaussian());
        }
        XYSeriesCollection dataset = new XYSeriesCollection(series);
        new Timer(500, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                series.add(series.getItemCount(), random.nextGaussian());
            }
        }).start();
        JFreeChart chart = ChartFactory.createXYLineChart("Test", "Domain",
            "Range", dataset, PlotOrientation.VERTICAL, false, false, false);
        return new ChartPanel(chart) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(480, 240);
            }
        };
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test().display();
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045