javaswingjtextarea

JTextArea not being appended to in swing GUI


I am currently working on a GUI calculator program using Swing. I got the actual interface done here

However, whenever I click on one of the number buttons, nothing appears in the operand text area, while 0.0 appears in the result text area as can be seen here.

Here is my code:

`
public class Calculator extends JFrame implements ActionListener
{
    private static JTextArea operandArea;
    private static JTextArea resultArea;
    private static JButton[] buttons = new JButton[10];

    public Calculator()
    {
        setTitle("Calculator");
        setSize(800, 800);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        BorderLayout calculatorLayout = new BorderLayout();
        setLayout(calculatorLayout);

        JPanel wholeCalculator = new JPanel();
        add(wholeCalculator);
        JPanel textAreas = new JPanel();
        wholeCalculator.add(textAreas, calculatorLayout.NORTH);
        textAreas.setPreferredSize(new Dimension(800, 300));
        JPanel numberPad = new JPanel();
        wholeCalculator.add(numberPad, calculatorLayout.SOUTH);
        numberPad.setPreferredSize(new Dimension(800, 300));

        operandArea = new JTextArea();
        operandArea.setPreferredSize(new Dimension(600,75));
        resultArea = new JTextArea();
        resultArea.setPreferredSize(new Dimension(600,75));

        textAreas.setLayout(new GridLayout(4, 1));
        JLabel operand = new JLabel("Operand");
        JLabel result = new JLabel("Result");
        textAreas.add(operand);
        operand.setHorizontalAlignment(JLabel.CENTER);
        textAreas.add(operandArea);
        textAreas.add(result);
        result.setHorizontalAlignment(JLabel.CENTER);
        textAreas.add(resultArea);

        numberPad.setLayout(new GridLayout(1, 2));

        JPanel numbers = new JPanel();
        numberPad.add(numbers);
        numbers.setLayout(new GridLayout(4, 3));
        numbers.setPreferredSize(new Dimension(400, 400));
        JPanel operations = new JPanel();
        numberPad.add(operations);
        operations.setPreferredSize(new Dimension(200, 400));
        JButton minus = new JButton("-");
        minus.setActionCommand("minus");
        operations.add(minus);
        JButton plus = new JButton("+");
        plus.setActionCommand("plus");
        operations.add(plus);
        JButton multiply = new JButton("X");
        multiply.setActionCommand("multiply");
        operations.add(multiply);
        JButton divide = new JButton("/");
        divide.setActionCommand("divide");
        operations.add(divide);
        JButton decimal = new JButton(".");
        operations.add(decimal);
        JButton reset = new JButton("reset");
        reset.setActionCommand("reset");
        operations.add(reset);
        JButton clear = new JButton("clear");
        operations.add(clear);

        for (int i = 1; i <= 9; i++)
        {
            JButton temp = new JButton(i + " ");
            buttons[i] = temp;
            temp.addActionListener(this);
            numbers.add(temp);
        }
            numbers.add(new JButton("0"));
    }

    public void actionPerformed(ActionEvent e)
    {
        String action = e.getActionCommand();
        double initializer = 0.0;
        String operand = "";

        resultArea.setText(Double.toString(initializer));

        switch (action)
        {
            case "0":
                operandArea.append("0");
                operand = operand + "0";
                break;
            case "1":
                operandArea.append("1");
                operand = operand + "1";
                break;
            case "2":
                operandArea.append("2");
                operand = operand + "2";
                break;
            case "3":
                operandArea.append("3");
                operand = operand + "3";
                break;
            case "4":
                operandArea.append("4");
                operand = operand + "4";
                break;
            case "5":
                operandArea.append("5");
                operand = operand + "5";
                break;
            case "6":
                operandArea.append("6");
                operand = operand + "6";
                break;
            case "7":
                operandArea.append("7");
                operand = operand + "7";
                break;
            case "8":
                operandArea.append("8");
                operand = operand + "8";
                break;
            case "9":
                operandArea.append("9");
                operand = operand + "9";
                break;
            case ".":
                operandArea.append(".");
                operand = operand + ".";
                break;
            case "plus":
                initializer += Double.parseDouble(operand);
                resultArea.setText(Double.toString(initializer));
                break;
            case "minus":
                initializer -= Double.parseDouble(operand);
                resultArea.setText(Double.toString(initializer));
                break;
            case "multiply": initializer *= Double.parseDouble(operand);
                resultArea.setText(Double.toString(initializer));
                break;
            case "divide":
                try
                {
                    if (operand == "0")
                    {
                        throw new DivideByZeroException();
                    }
                    initializer /= Double.parseDouble(operand);
                    resultArea.setText(Double.toString(initializer));
                }
                catch (DivideByZeroException ex)
                {
                    resultArea.setText("Cannot divide by 0");
                }
                break;
            case "clear":
                operandArea.setText("");
                break;
            case "reset":
                operandArea.setText("");
                resultArea.setText("");
                initializer = 0.0;
                break;
        }
    }

    public static void main(String[] args)
    {
        Calculator frame = new Calculator();
        frame.setVisible(true);
    }
}
`

I thought using the append command would add to the blank space? I would appreciate any input.


Solution

  • Your issue is a compound one.

    First, you create the button using...

    JButton temp = new JButton(i + " ");
    

    This, by default, sets the actionCommmand to i_ <- not the space! (So won't let me put in a trailing space, so I've use _ to highlight)

    You then use

        switch (action)
        {
            case "0":
            //...
    

    To check the actionCommand, but the command isn't (as an example) [0], it's [0_]

    Instead, use something like...

    JButton temp = new JButton(Integer.toString(i));
    

    You're also going to want to fix:

    if (operand == "0")
    

    as == is not how you should be comparing Strings (and a good IDE should be warning you about it). Instead useequals`, for example...

    if ("0".equals(operand))