Upon reading up on lots of question being posted and answered by people, I still don't get how the passing of data works in Java. I have a simple code right here that require the user to type in their age, height, weight and a button that will open up the 2nd Frame.
First Frame
public Collectdata()
{
JPanel text = new JPanel();
text.add(jage);
text.add(age);
text.add(jheight);
text.add(height);
text.add(jweight);
text.add(weight);
text.setLayout(new GridLayout(3,2));
JPanel jbutt = new JPanel();
jbutt.add(next);
setLayout(new BorderLayout());
add(text,BorderLayout.CENTER);
add(jbutt,BorderLayout.SOUTH);
next.addActionListener(this);
}
public static void main(String[] args)
{
Collectdata GUI = new Collectdata();
GUI.setTitle("DataCollection"); //Set title
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close program
GUI.setSize(250,150);
GUI.setVisible(true);
}
public void actionPerformed(ActionEvent a)
{
if(a.getSource() == next)
{
Calculate secondwind = new Calculate(this);
secondwind.setTitle("Calculate"); //Set title
secondwind.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close program
secondwind.setSize(350,150);
secondwind.setVisible(true);
this.setVisible(false);
}
}
}
And this is the 2nd Frame that will be shown after clicking the button.
2nd Frame
public Calculate(Collectdata collectdata)
{
this.collectdata = collectdata;
JPanel text = new JPanel();
text.add(jage);
text.add(age);
text.add(jheight);
text.add(height);
text.add(jweight);
text.add(weight);
text.setLayout(new GridLayout(3,2));
age.setEditable(false);
height.setEditable(false);
weight.setEditable(false);
setLayout(new BorderLayout());
add(text,BorderLayout.CENTER);
}
I will like to be able to get the data keyed from the JFrame to be able to pass over to the 2nd JFrame and using the data for some calculation.
How do I do that?
Thanks.