I tried to display a JFreeChart inside a JDialog and it is displaying correctly but when i tried to display it in a modal JDialog it is not dispaying.Following program is not displaying JFreeChart graph until i comment line# 31. What is the reason?
import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.Dimension;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GraphExample extends JDialog{
private final JPanel contentPanel = new JPanel(new BorderLayout());
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
GraphExample graph=new GraphExample(new JFrame(),"Testing");
graph.createGraphPanel();
} catch (Exception e) {
e.printStackTrace();
}
}
public GraphExample(JFrame parent,String title) {
super(parent,title);
setPreferredSize(new Dimension(500, 500));
this.setModal(true);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(contentPanel, BorderLayout.CENTER);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
pack();
setVisible(true);
}
public void createGraphPanel(){
XYSeriesCollection dataset = new XYSeriesCollection();
XYSeries series1= new XYSeries("First");
series1.add(1.0, 1.0);
series1.add(2.0, 4.0);
series1.add(3.0, 3.0);
series1.add(4.0, 5.0);
series1.add(5.0, 5.0);
series1.add(6.0, 7.0);
series1.add(7.0, 7.0);
series1.add(8.0, 8.0);
dataset.addSeries(series1);
final JFreeChart chart = createChart(dataset);//,xLabel,yLabel,title,seriesesVector,check);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(400,400));//width height
contentPanel.add(chartPanel,BorderLayout.CENTER);
contentPanel.revalidate();
}
private JFreeChart createChart(final XYDataset dataset){//, String xLabel, String yLabel, String title,Vector seriesesVector,String check) {
final JFreeChart result = ChartFactory.createXYLineChart(//createXYAreaChart(//also comment line#218 219 220 also uncomment line#250
null,
"X",
"Y",
dataset,
PlotOrientation.VERTICAL,
true,
true,
false
);
result.setTitle("Example");
final XYPlot plot = result.getXYPlot();//domain is x and range is y
plot.setDomainGridlinesVisible(false);
plot.setRangeGridlinesVisible(false);
return result;
}
}
How to show a JFreeChart in a Modal JDialog?
Don't call setVisible(true)
on the JDialog until everything is set up. In fact, get it out of the constructor, and call it yourself in the code that declares and constructs the JDialog and only after all is done. Understand that code flow will be blocked when it is called, and so it must be called last.
So change this:
public GraphExample(JFrame parent,String title) {
super(parent,title);
setPreferredSize(new Dimension(500, 500));
this.setModal(true);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(contentPanel, BorderLayout.CENTER);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
pack();
setVisible(true);
}
to this:
public GraphExample(JFrame parent,String title) {
super(parent,title);
setPreferredSize(new Dimension(500, 500));
this.setModal(true);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(contentPanel, BorderLayout.CENTER);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
pack();
// setVisible(true); // **** **** **** ****
}
and this:
public static void main(String[] args) {
try {
GraphExample graph=new GraphExample(new JFrame(),"Testing");
graph.createGraphPanel();
graph.setVisible(true); // ****** add *****
} catch (Exception e) {
e.printStackTrace();
}
}