I'm trying to display a set of lines from two points after a second.However when I run the piece of code as shown below..I get a set of JFrames which are initially empty.Only when the "for" loop is complete,I see the data on these frames.Can you please help me out as to how to show data on the frames when each frame is individually shown to the user...thanks in advance.
public class XYLineChartExample extends JFrame {
static ChartPanel panel ;
static JFreeChart chart;
public XYLineChartExample(String title,int i) throws InterruptedException {
super(title);
XYDataset dataset = createDataset(i);
// Create chart
chart = ChartFactory.createXYLineChart(
"XY Line Chart Example",
"X-Axis",
"Y-Axis",
dataset,
PlotOrientation.VERTICAL,
true, true, false);
// Create Panel
panel = new ChartPanel(chart);
setContentPane(panel);
}
private XYDataset createDataset(int i) {
XYSeriesCollection dataset = new XYSeriesCollection();
XYSeries series = new XYSeries("Y = X + 2");
series.add(i,i+2 );
series.add(i+3,i+5 );
//Add series to dataset
dataset.addSeries(series);
return dataset;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
for(int i=1;i<5;i++){
try {
XYLineChartExample example;
example = new XYLineChartExample("XY Chart Example",i);
example.setSize(1000, 600);
example.setLocationRelativeTo(null);
example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
example.setVisible(true);
Thread.sleep(2000);
// panel.removeAll();
// panel.add(example);
// panel.updateUI();
// example.removeAll();
// example.add(chart);
// example.dispose();
} catch (InterruptedException ex) {
Logger.getLogger(XYLineChartExample.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
}