I am working on a program that has a gui. the user enters a number which the action listener saves in a variable and then sends this variable to a class main in which it is stored in another variable. The problem is that the way i am currently doing this is not saving the value properly(sometimes the vaue saves and sometimes it doesn't. i want that when the user enters the value in gui and then presses the button this value be saved in a variable in main class. I am very new to java so please excuse my awful coding. This is my main class.
public class Main {
public static boolean Click=false;
public static void main(String[] args) {
int n=0, i;
JFrame Frame = new JFrame();
Input1 I1 = new Input1();
Frame.setSize(600, 600);
Frame.setLocationRelativeTo(null);
Frame.setContentPane(I1.Input1Panel);
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame.pack();
Frame.setVisible(true);
while (Click==false) {
n = I1.Setn();
System.out.println(""+n);
}
And This is my gui class.
public class Input1 {
private JButton doneButton;
private JTextField textField1;
private JLabel Title;
private JLabel EnterNum;
public JPanel Input1Panel;
public int n;
Main M = new Main();
public Input1() {
doneButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
n = Integer.parseInt(textField1.getText());
while (M.Click==false) {
if (n==0) {
n = Integer.parseInt(textField1.getText());
}
else {
M.Click=true;
}
}
}
});
}
public int Setn() {
return n;
}
}
New code Main Class
public class Main {
public static void main(String[] args) {
int n=0, i;
JFrame Frame = new JFrame();
Input1 I1 = new Input1();
Frame.setSize(600, 600);
Frame.setLocationRelativeTo(null);
Frame.setContentPane(I1.Input1Panel);
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame.pack();
Frame.setVisible(true);
while (n==0) {
n = I1.Setn();
//System.out.println(""+n);
}
System.out.println("Main:"+n);
GUI class
public class Input1 {
private JButton doneButton;
private JTextField textField1;
private JLabel Title;
private JLabel EnterNum;
public JPanel Input1Panel;
public int n;
Main M = new Main();
public Input1() {
doneButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
n = Integer.parseInt(textField1.getText());
System.out.println("Action Listener:"+n);
}
});
}
public int Setn() {
return n;
}
}