简体   繁体   中英

I am having trouble getting a ChartPanel to appear in an existing jPanel. Does this have to do with Matisse?

this being my first question on Stack Overflow, I apologize for any mistakes in describing the problem, so feel free to ask for more or a different description.

I am making a program with the jFreeChart API where I'm trying to allow for live editing of displayed charts by creating a ChartPanel and refreshing the chart inside it each time an edit is made.

public void displayChart(JFreeChart chart) {
    ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setDomainZoomable(true);
    chartPanel.setVisible(true);
    jPanel3.add(chartPanel, BorderLayout.CENTER);
    jPanel3.setLayout(new java.awt.BorderLayout());
    jPanel3.validate();
}

private void ChartSelectionActionPerformed(java.awt.event.ActionEvent evt) {                                               
    String selection;
    selection = ChartSelection.getActionCommand();
    if (selection == "S/D") {
    Chart Chart = new Chart();
    JFreeChart chart = Chart.generateSandDBase();
    displayChart(chart);
    } else {

    }
}                                              

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Series Series = new Series();
    Chart Chart = new Chart();
    XYSeries supply = Series.getSupplySeries();
    double tempX;
    double tempY;
    for (int i = 0; i < 10; i++) {
        tempX = (double) supply.getX(i);
        tempX = tempX + 1;
        tempY = (double) supply.getY(i);
        supply.remove(i);
        supply.add(tempX, tempY);
        System.out.println(supply.getX(i).toString());
        System.out.println(supply.getY(i).toString());
    }
    Series.setSupplySeries(supply);
    JFreeChart chart = Chart.generateSandD(supply, Series.getDemandSeries());
    displayChart(chart);
}                   

I have found similar questions asked but all of the solutions I have found haven't worked in solving this issue. At runtime, the chart simply doesn't appear in the panel. I have tried adding a new panel inside the existing one, then adding the ChartPanel inside that. I have tried validating the entire JFrame, and also scrubbing the Jpanel before adding my ChartPanel, and then validating all of it after that.

I am a little worried that this might have something to do with Matisse, as I am using NetBeans' GUI builder which seems to run on some dark space magic that could be screwing this all up.

Thanks so much for any help, I am a novice programmer, so it all goes a really long way.

Edit: So, after a few more hours of messing around with everything, I heard something about violating hierarchy when I try to add a chart at runtime. I don't know very much about this. Is there a way for me to instantiate my ChartFrame in the GUI constructor? I know where to put the code, but I can't seem to get this working as a chart frame must have a chart to be instantiated.

So after a couple of terrible, terrible hours scouring the internet, I found the problem.

Turns out that in order to have a ChartPanel in a JPanel, the JPanel must be in BorderLayout. I had already tried doing this a couple of times before, but the default Swing GUI builder in NetBeans, Matisse, seems to not allow the changing of the layout of a placed component at runtime.

I had to set the JPanel to BorderLayout in the Matisse design tab, by right-clicking on the JPanel and setting the layout to border. Matisse for some reason decides to resize the panel to (0,0), so that has to get resized as well.

Other than that my code was correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM