I’m having an issue retrieving a values from my getters and setters. I’m setting the value in one class and then trying to retrieve it in another so that it can be used in a label. I haven’t done any programming in Java in a while so I feel that there is something fundamental that I am not aware of.
I’ve written this short code to display the problem I’m having in my main project. Any help will be appreciated, I hope you have a good day.
Form1 Class:
public class Form1 extends JFrame implements ActionListener {
JButton btn1;
JTextField tf1;
public Form1() {
btn1 = new JButton("submit");
btn1.setBounds(75, 90, 100, 40);
btn1.setFocusable(false);
btn1.addActionListener(this);
tf1 = new JTextField();
tf1.setBounds(20, 30, 200, 40);
btn1.addActionListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(250, 180);
this.setLayout(null);
this.setResizable(false);
this.setVisible(true);
this.add(btn1);
this.add(tf1);
}
public void actionPerformed(ActionEvent e) {
String b = "";
String v = "";
String m = "";
String r = "";
Cars car = new Cars(b, v, m, r);
if(e.getSource()==btn1) {
b = tf1.getText();
car.SetBmw(b);
new Form2();
System.out.println(car.GetBmw());
// Side note, for some reason this will be printed twice in the console.
}
}
}
Form2 Class:
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Form2 extends JFrame {
Cars car = new Cars();
public Form2() {
JLabel label = new JLabel();
label.setText(car.GetBmw());
label.setBounds(10, 10, 50, 50);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(250, 180);
this.setLayout(null);
this.setResizable(true);
this.setVisible(true);
this.add(label);
}
}
Cars Class:
public class Cars {
private String bmw;
private String vw;
private String mazda;
private String renault;
public Cars() {}
public Cars(String bmw, String vw, String mazda, String renault) {
this.bmw = bmw;
this.vw = vw;
this.mazda = mazda;
this.renault = renault;
}
// Getters and Setters for BMW
public String GetBmw() {
return bmw;
}
public void SetBmw(String bmw) {
this.bmw = bmw;
}
// Getters and Setters for VW
public String GetVw() {
return vw;
}
public void SetVw(String vw) {
this.vw = vw;
}
// Getters and Setters for Mazda
public String GetMazda() {
return mazda;
}
public void SetMazda(String mazda) {
this.mazda = mazda;
}
// Getters and Setters for Renault
public String GetRenault() {
return renault;
}
public void SetRenault(String renault) {
this.renault = renault;
}
}
>Solution :
You are creating a new instance of Car in Form2 which is empty, and you are using getters on this empty instance.
To retrieve data on instance from Form1, you have to pass it to Form2, through constructor for example:
public void actionPerformed(ActionEvent e) {
String b = "";
String v = "";
String m = "";
String r = "";
Cars car = new Cars(b, v, m, r);
if(e.getSource()==btn1) {
b = tf1.getText();
car.SetBmw(b);
new Form2(car);
System.out.println(car.GetBmw());
// Side note, for some reason this will be printed twice in the console.
}
}
public class Form2 extends JFrame {
public Form2(Car car) {
JLabel label = new JLabel();
label.setText(car.GetBmw());
label.setBounds(10, 10, 50, 50);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(250, 180);
this.setLayout(null);
this.setResizable(true);
this.setVisible(true);
this.add(label);
}
}