views:

53

answers:

1

This is similar to a question I asked yesterday but more specific to the problem. What is the correct method to add a JFreeChart to a NetBeans project which already contains various widgets? My updateChart() hides the entire JFrame. I'd like to add the JFreeChart to the JFrame.

public class MyClass extends javax.swing.JFrame implements TableModelListener {
    public MyClass() {
        initComponents();
        ...
        updateChart();
    }
    private void updateChart() {
        XYDataset dataset = createXYdataset();
        JFreeChart chart = createChart(dataset);
        JPanel chartPanel = new ChartPanel(chart);
        setContentPane(chartPanel);
    }
    private XYDataset createXYdataset() {
        XYSeries series = new XYSeries("");
        int rows = jTable.getRowCount();
        if (rows > 0) {
            int ms = 0;
            for (int row = 0; row < rows; row++) {
                series.add(ms, 1);
                ms += Integer.parseInt(
                        jTable.getValueAt(row, PULSE_ON).toString());
                series.add(ms, 1);
                series.add(ms, 0);
                ms += Integer.parseInt(
                        jTable.getValueAt(row, PULSE_OFF).toString());
                series.add(ms, 0);
            }
        }
        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);
        return dataset;
    }
    private JFreeChart createChart(XYDataset dataset) {
        JFreeChart chart = ChartFactory.createXYLineChart(
            null,      // chart title
            "ms",                      // x axis label
            null,                      // y axis label
            dataset,                  // data
            PlotOrientation.VERTICAL,
            false,                     // include legend
            true,                     // tooltips
            false                     // urls
        );
        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setDomainPannable(true);
        plot.setRangePannable(true);
        plot.setRangeGridlinesVisible(false);
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        return chart;
    }
}

Corrected code:

private void updateChart() {
    XYDataset dataset = createXYdataset();
    JFreeChart chart = createChart(dataset);
    JPanel chartPanel = new ChartPanel(chart);
    chartPanel.setSize(jPanel1.getSize());
    jPanel1.add(chartPanel);
    jPanel1.getParent().validate();
}
+3  A: 

My updateChart() hides the entire JFrame.

JFreeChart chart = createChart(dataset); 
JPanel chartPanel = new ChartPanel(chart); 
setContentPane(chartPanel); 

That would be because you are replacing the content pane of your frame with the panel from free chart.

I don't know what layout manager you are using, but you need to "ADD" the free chart panel to the panel that contains all the other components. So maybe when you design the general form in Netbeans you add an empty panel to the place where you want the free chart panel to be added. Then when you add the free chart panel the code would be something like:

emptyFreeChartPanel.add( chartPanel );
emptyFreeChartPanel.getParent().validate();

The validate tells Swing that components have been added so the layout manager will be invoked.

camickr
The NetBeans IDE seems to be using a GroupLayout for the LayoutManager. I added a JPanel called jPanel1 to the project and `jPanel1.add(chartPanel); jPanel1.getParent().validate(); and jPanel1.setVisible(true);` to `updateChart()` but the chart is not visible in jPanel1. Any other ideas?
JackN
GroupLayout requires the use of constraints which I know nothing about. I guess the problem is that the empty panel has a 0 preferred size so the contraints aren't set properly to add another component to it. I always build my GUI layouts by hand so I am in full control of the layout. So my suggestion would be to learn how to use layout managers and do the coding yourself then you are not dependent on the IDE.
camickr
Thanks a million for the clue. All it needed was a `chartPanel.setSize(jPanel1.getSize());`.
JackN