Jelly
Jelly

Reputation: 87

Keep variable up to date in JTextField - Java

So I'm very new to java and I'm trying to may a GUI calculator using buttons. When the user presses a button I want it to add the number they pressed onto the first number so it works something like this: GUI Example

The problem I'm having is that JTextField doesn't update when the event handler adds the numbers to the total. My code is as follows:

    package App;

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;


    public class gui_calc extends JFrame
    {
        private Button button0;
        private Button button1;
        private Button button2;
        private Button button3;
        private Button button4;
        private Button button5;
        private Button button6;
        private Button button7;
        private Button button8;
        private Button button9;
        private Button buttonNull;
        private Button go;
        private String  num1_sum = "0";
        private String num2_sum = "0";
        private JTextField sum;


        public gui_calc()
        {

            super("Title");

            Label label1 = new Label("Enter First Number");

            JPanel numPad1 = new JPanel();
            button0 = new Button("0");
            button1 = new Button("1");
            button2 = new Button("2");
            button3 = new Button("3");
            button4 = new Button("4");
            button5 = new Button("5");
            button6 = new Button("6");
            button7 = new Button("7");
            button8 = new Button("8");
            button9 = new Button("9");
            buttonNull = new Button("-");
            go = new Button("Next");
            numPad1.setLayout(new GridLayout(4,3));
            numPad1.add(button1);
            numPad1.add(button2);
            numPad1.add(button3);
            numPad1.add(button4);
            numPad1.add(button5);
            numPad1.add(button6);
            numPad1.add(button7);
            numPad1.add(button8);
            numPad1.add(button9);
            numPad1.add(buttonNull);
            numPad1.add(button0);
            numPad1.add(go);


            eventHandler handler = new eventHandler();
            button1.addActionListener(handler);

            add(numPad1);
    // Where the number total is displayed
            sum = new JTextField(num1_sum);
            sum.setColumns(10);
            add(sum);




        }



        private class eventHandler implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("Event");

                if (e.getSource()==button0)
                {
                    num1_sum = num1_sum + "0";
                }

                else if (e.getSource()==button1)
                {
                    num1_sum = num1_sum + "1";
// Creating the total and then printing to console so I know everything works as I // want it to
                    System.out.println(num1_sum);
                }

                else if (e.getSource()==button2)
                {
                    num1_sum = num1_sum + "2";

                }

                else if (e.getSource()==button3)
                {
                    num1_sum = num1_sum + "3";
                }

                else if (e.getSource()==button4)
                {
                    num1_sum = num1_sum + "4";
                }

            }
        }
    }

I am using strings for the digits because obviously if the user pressed 1 and 4 with integers then I'd get 5 when adding them. I'm going to parse them Integer when I do the calculations.

After the user presses next, the application will then go to a second JPanel where they enter a second number in the same way and then I hope to display the result after next is pressed again.

I haven't got to this stage however because in testing I found that JTextField doesn't update when a button is pressed but when I mash the number 1 button the console outputs 01, 011, 0111, etc. I've only got a listener on button 1 for now just because I want to get one button working before doing the rest.

I know this code is probably terrible and it could easily be done far more coherently but I am still very new to java. Thanks for any help

Upvotes: 0

Views: 56

Answers (1)

lostbard
lostbard

Reputation: 5230

With only one buttons events enabled, you are only going to see results work for one button. It looks like the code as is close to working, but you need to handle the leading zero, so that if you press a number and the current num1_sum is "0", just set num1_sum to the new value.

and then call the jtextfields setText method as sum.setText(num1_sum);

Upvotes: 1

Related Questions