Herman Eriksson Herman Eriksson - 29 days ago 10
Java Question

JTextField not responding to JButtons (Solved)

So, I am making a calculator for a project in school and I get a major problem. The calculator worked and all but then I added a method to make calculating easier and to make it accept more than two numbers. After that my buttons stoped working. I have tried a few things and I know that the code is reachable. There are no errors displaying either. If you know something, please help. Feel free to ask questions and I will try to answer them.

Cheers!

Problem:

Number buttons refuse to print numbers into JTextField.

Code:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Calculator extends JFrame implements ActionListener {

JLabel lbl1;
JTextField tf;
JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bAdd, bEqu, bSub, bMult, bDiv, bC, bBS, bDot;
static double a=0, b=0, c;
static int lastOperator = 0; // 1 = +, 2 = -, 3 = *, 4 = /.

// Display everything in the window
public Calculator() {
lbl1 = new JLabel(); tf = new JTextField(10);b0 = new JButton("0");
b1 = new JButton("1"); b4 = new JButton("4"); b7 = new JButton("7");
b2 = new JButton("2"); b5 = new JButton("5"); b8 = new JButton("8");
b3 = new JButton("3"); b6 = new JButton("6"); b9 = new JButton("9");
bAdd = new JButton("+"); bSub = new JButton("-"); bMult = new JButton("*"); bDot = new JButton(".");
bEqu = new JButton("="); bC = new JButton("C"); bBS = new JButton("CE"); bDiv = new JButton("/");

tf.setBounds(30,40,350,30); bBS.setBounds(40,100,50,100); bC.setBounds(40,220,50,100);
b7.setBounds(110,100,50,40); b8.setBounds(180,100,50,40); b9.setBounds(250,100,50,40); bDiv.setBounds(320,100,50,40);
b4.setBounds(110,160,50,40); b5.setBounds(180,160,50,40); b6.setBounds(250,160,50,40); bMult.setBounds(320,160,50,40);
b1.setBounds(110,220,50,40); b2.setBounds(180,220,50,40); b3.setBounds(250,220,50,40); bSub.setBounds(320,220,50,40);
bDot.setBounds(110,280,50,40); b0.setBounds(180,280,50,40); bEqu.setBounds(250,280,50,40); bAdd.setBounds(320,280,50,40);

add(tf); add(b7); add(b8); add(b9); add(b4); add(b5); add(b6); add(b1); add(b2); add(b3); add(b0);
add(bDot); add(bAdd); add(bSub); add(bMult); add(bDiv); add(bEqu); add(bC); add(bBS);

b0.addActionListener(this); b4.addActionListener(this); b8.addActionListener(this);
b1.addActionListener(this); b5.addActionListener(this); b9.addActionListener(this);
b2.addActionListener(this); b6.addActionListener(this); bAdd.addActionListener(this);
b3.addActionListener(this); b7.addActionListener(this); bEqu.addActionListener(this);
bSub.addActionListener(this); bMult.addActionListener(this); bC.addActionListener(this);
bBS.addActionListener(this); bDiv.addActionListener(this); bDiv.addActionListener(this);
bDot.addActionListener(this);
}

// Metod for calculating
public void operatorMethod(int lastOperator) {
if (lastOperator > 0) {
b = Double.parseDouble(tf.getText());
}

switch (lastOperator) {
case 0:
a = Double.parseDouble(tf.getText());
break;
case 1:
a = a +b;
break;
case 2:
a = a-b;
break;
case 3:
a = a * b;
break;
case 4:
a = a / b;
break;
default:
a = 0;
break;
}
tf.setText("");
}

// Action Listener
public void actionPerformed(ActionEvent e) {

// Input
if (e.getSource() == b0) {
tf.setText(tf.getText().concat("0"));
}
if (e.getSource() == b1) {
tf.setText(tf.getText().concat("1"));
}
if (e.getSource() == b2) {
tf.setText(tf.getText().concat("2"));
}
if (e.getSource() == b3) {
tf.setText(tf.getText().concat("3"));
}
if (e.getSource() == b4) {
tf.setText(tf.getText().concat("4"));
}
if (e.getSource() == b5) {
tf.setText(tf.getText().concat("5"));
}
if (e.getSource() == b6) {
tf.setText(tf.getText().concat("6"));
}
if (e.getSource() == b7) {
tf.setText(tf.getText().concat("7"));
}
if (e.getSource() == b8) {
tf.setText(tf.getText().concat("8"));
}
if (e.getSource() == b9) {
tf.setText(tf.getText().concat("9"));
}
if (e.getSource() == bDot) {
tf.setText(tf.getText().concat("."));
}

// Counting
// Add
if (e.getSource() == bAdd) {
operatorMethod(lastOperator);
lastOperator = 1;
}

// Subtract
if (e.getSource() == bSub) {
operatorMethod(lastOperator);
lastOperator = 2; }

// Mutliply
if (e.getSource() == bMult); {
operatorMethod(lastOperator);
lastOperator = 3;
}

// Divide
if (e.getSource() == bDiv) {
operatorMethod(lastOperator);
lastOperator = 4;
}

// Different operations E.g =.
if (e.getSource() == bEqu) {
operatorMethod(lastOperator);
lastOperator = 0;
tf.setText("" + a);
}

if (e.getSource() == bBS) {
String s = tf.getText();
tf.setText("");
for (int i = 0; i < s.length()-1; i++) {
tf.setText(tf.getText()+s.charAt(i));
}
}
if (e.getSource() == bC) {
tf.setText("");
}

}

// Create window
public static void main(String[] args) {
Calculator f = new Calculator();
f.setLayout(null);
f.setLocation(1100,300);
f.setTitle("Calculatoren!!!!");
f.setVisible(true);
f.setSize(410,410);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
}
}

Answer

There is an unnecessary ; in this line: if (e.getSource() == bMult); {.

With this extra character the if contains an empty statement and the following code block ({ operatorMethod(lastOperator); lastOperator = 3; }) is executed after every button press and operatorMethod always deletes the textfield's content.

Remove the ; and the problem will disappear.