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...