problems displaying chart
I'm using JFreeChart to create a a chart and then I create a ChartPanel to display the chart. The code is part of a class called MassSweep:
Code:
private PortHandler portHandler;
private DefaultXYDataset data = new DefaultXYDataset();
private JFreeChart graph = ChartFactory.createXYLineChart("Mass Sweep",
"Pressure (Torr)", "Mass (amu)", data, PlotOrientation.HORIZONTAL,
false, true, false);
Later in another class which is a JFrame, I have a panel within the frame that I've already added to the frame. I add the ChartPanel to the panel which is on the frame. So I thought it would work to do this: Code:
jPanel1.add(massSweep.display());
But when I run my program, the chart does not display.
This is the code generated automatically by NetBeans (the IDE). I don't get it...
Code:
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 1045, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 361, Short.MAX_VALUE)
);
Re: problems displaying chart
To start off are you able to display a background color in your JPanel? it's a good start...
Re: problems displaying chart
If you're going to be adding components on the fly at run-time, you'll need to have your containers (here your JPanel that is accepting the new component) use layout managers that can handle this better. If you haven't yet done so, I suggest that you read the layout manager tutorials in the Swing tutorials.
Re: problems displaying chart
If you add/remove components to a container that is already realized (eg visible), you must revalidate or validate the container (this is clearly defined in the API for the add method of container - http://docs.oracle.com/javase/6/docs...t.Component%29 ). Try calling revalidate() pr validate() followed by repaint() on the JPanel you add the Chart to...but as Fubarable mentioned, there are perhaps other alternatives you can use to facilitate what you need (for instance, a CardLayout)
Re: problems displaying chart
Thanks all, for your input.