Asunez
Asunez

Reputation: 2347

Java Swing passing variables to JFreeChart class

I need help with last one thing in my project. I need to pass 4 variables (named a, b, c and d) from JDialog to JFreeChart in another class. I have added checkpoints to see where it actually stops to be processed and the code is as follows:

package calc.kalkulator;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

class PanelFunkcji extends JDialog implements ActionListener {
    private JLabel lA, lB, lC, lD;
    private JTextField tA, tB, tC, tD;
    private JButton bOK, bCancel;
    private OknoFunkcji oknoFunkcji;
    public double dA, dB, dC, dD;






public PanelFunkcji(JFrame owner) {
    super(owner, "Wprowadzanie funkcji", true);
    setSize(300, 300);
    setLayout(null);

    bOK = new JButton("OK");
    bOK.setBounds(10, 40, 130, 20);
    add(bOK);
    bOK.addActionListener(this);

    bCancel = new JButton("Anuluj");
    bCancel.setBounds(150, 40, 130, 20);
    add(bCancel);
    bCancel.addActionListener(this);

    lA = new JLabel("A = ");
    lA.setBounds(10, 70, 40, 20);
    add(lA);

    tA = new JTextField("0");
    tA.setBounds(50, 70, 60, 20);
    add(tA);

    lB = new JLabel("B = ");
    lB.setBounds(10, 100, 40, 20);
    add(lB);

    tB = new JTextField("0");
    tB.setBounds(50, 100, 60, 20);
    add(tB);

    lC = new JLabel("C = ");
    lC.setBounds(10, 130, 40, 20);
    add(lC);

    tC = new JTextField("0");
    tC.setBounds(50, 130, 60, 20);
    add(tC);

    lD = new JLabel("D = ");
    lD.setBounds(10, 160, 40, 20);
    add(lD);

    tD = new JTextField("0");
    tD.setBounds(50, 160, 60, 20);
    add(tD);

}

public double getA() {
    return Double.parseDouble(tA.getText());
}

public double getB() {
    return Double.parseDouble(tB.getText());
}

public double getC() {
    return Double.parseDouble(tC.getText());
}

public double getD() {
    return Double.parseDouble(tD.getText());
}

@Override
public void actionPerformed(ActionEvent e) {
    Object z = e.getSource(); 
    if(z == bOK) {
        dA = getA();
        dB = getB();
        dC = getC();
        dD = getD();
        System.out.println("1st checkpoint: A " + dA + " B " + dB + " C " + dC + " D " + dD);
        if (oknoFunkcji == null) {
            oknoFunkcji = new OknoFunkcji("Wykres");
        }
        oknoFunkcji.setVisible(true);
        //setVisible(false);
    }
    else if (z == bCancel) {
        setVisible(false);          
    }   
}   
}

The class I am trying to pass the variables to is this one:

package calc.kalkulator;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.*;

class OknoFunkcji extends ApplicationFrame {

private static PanelFunkcji panelFunkcji;
private static double a;
private static double b;
private static double c;
private static double d;

public void getDane () {
    a = panelFunkcji.dA;
    b = panelFunkcji.dB;
    c = panelFunkcji.dC;
    d = panelFunkcji.dD;
    System.out.println("2nd checkpoint: A " + a + " B " + b + " C " + c + " D " + d);
}


public OknoFunkcji(String title) {
    super(title);
    JPanel chartPanel = createDemoPanel();

    panelFunkcji = new PanelFunkcji(new JFrame()); 
    chartPanel.setSize(500, 270);
    setContentPane(chartPanel);
    getDane();
    System.out.println("3rd checkpoint: A " + a + " B " + b + " C " + c + " D " + d);


}

/**
 * Tworzy chart.
 * 
 * @param dataset - zbiór danych.
 * 
 * @return Zwraca instancję charta.
 */
private static JFreeChart createChart(XYDataset dataset) {
    // create the chart...
    JFreeChart chart = ChartFactory.createXYLineChart(
        " ",       // chart title
        "X",                      // x axis label
        "Y",                      // y axis label
        dataset,                  // data
        PlotOrientation.VERTICAL,  
        true,                     // include legend
        true,                     // tooltips
        false                     // urls
    );

    XYPlot plot = (XYPlot) chart.getPlot();
    plot.getDomainAxis().setLowerMargin(0.0);
    plot.getDomainAxis().setUpperMargin(0.0);
    return chart;
}

/**
 * Creates a sample dataset.
 * 
 * @return A sample dataset.
 */
public static XYDataset createDataset() {
    XYDataset result = DatasetUtilities.sampleFunction2D(new X2(), 
            -10.0, 10.0, 40, "f(x)");
    return result;
}

public static JPanel createDemoPanel() {
    JFreeChart chart = createChart(createDataset());
    return new ChartPanel(chart);
}

static class X2 implements Function2D {

    public double getValue(double x) {
        return a*x*x*x + b*x*x + c*x + d;
    }

}



}

And what I get printed in console is this:

 1st checkpoint: A 1.0 B 2.0 C 3.0 D 4.0
 2nd checkpoint: A 0.0 B 0.0 C 0.0 D 0.0
 3rd checkpoint: A 0.0 B 0.0 C 0.0 D 0.0

So it is not actually the parsing that's making things go bad, and either it is REALLY simple and I am just missing a point or it is impossible...

Upvotes: 0

Views: 337

Answers (1)

nachokk
nachokk

Reputation: 14413

You are missing the point. The problem is that you have a reference in OknoFunkcji to PanelFunkcji.

Here you are creating a new instance, and you don't want to have a new instance of this JPanel. Besides you are not adding it to any container.

panelFunkcji = new PanelFunkcji(new JFrame()); 

This class PanelFunkcji use OknoFunkcji and not viceversa. So when you create a new instance of OknoFunkcji you have to pass that values via constructor injection or setter injection. For example you could do something like this.

@Override
public void actionPerformed(ActionEvent e) {
    Object z = e.getSource(); 
    if(z == bOK) {
        dA = getA();
        dB = getB();
        dC = getC();
        dD = getD();
        System.out.println("1st checkpoint: A " + dA + " B " + dB + " C " + dC + " D " + dD);
        if (oknoFunkcji == null) {
            oknoFunkcji = new OknoFunkcji("Wykres");
        }
        oknoFunkcji.createChart(dA,dB,dC,dD); // added this method 
        oknoFunkcji.setVisible(true);
        //setVisible(false);
    }
    else if (z == bCancel) {
        setVisible(false);          
    }   
}  

And in your another class

  public OknoFunkcji(String title) {
         super(title);
         System.out.println("3rd checkpoint: A " + a + " B " + b + " C " + c + " D " + d);
  }

  public void createChart(double dA, double dB, double dC, double dD) {
        setData(dA,dB,dC,dD);
        JPanel chartPanel = createDemoPanel();
        chartPanel.setSize(500, 270);
        setContentPane(chartPanel);
        revalidate();
        repaint();        
    }

    public void setData(double dA, double dB, double dC, double dD) {
        a = dA;
        b = dB;
        c = dC;
        d = dD;
        System.out.println("2nd checkpoint: A " + a + " B " + b + " C " + c + " D " + d);
    }

As a side note and not less important you should not make your variables and all your methods static cause then there is no practical reason to create instances of that class. Don't use null layout learn about using Layout Manager.

Upvotes: 2

Related Questions