I made a GUI which basically adds the numbers from the two textfield. The problem is, if I leave the other textfield blank and the other textfield has a number, the result textfield is not updating.
public class AdditionGui extends JFrame implements ActionListener { private TextField tf1 , tf2 , tf3; private Label sign, equalsign; private int sum = 0; AdditionGui(){ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new FlowLayout()); tf1 = new TextField(5); add(tf1); tf1.addActionListener(this); sign = new Label("+"); add(sign); tf2 = new TextField(5); add(tf2); tf2.addActionListener(this); equalsign = new Label("="); add(equalsign); tf3 = new TextField(5); add(tf3); setTitle("Sum of two numbers"); setSize(220,120); setVisible(true); } public void actionPerformed(ActionEvent ae) { int val1 = Integer.parseInt(tf1.getText()); int val2 = Integer.parseInt(tf2.getText()); sum = val1 + val2; tf3.setText("" + sum); } }