Search code examples
javagettextsettext

setText issue using getActionCommand


I am a beginner programmer trying to make a simple calculator. Towards the end of my code I try and have the text field show whatever button the user presses. For some reason the text.setText("7") does not show up in the text field. Could you please help me with this?

    import javax.swing.*; //imports all that is needed for the code
    import java.awt.*;
    import java.awt.event.*;

    public class Calculator extends JFrame implements ActionListener {

        public JButton button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12, button13, button14, button15, button16;
        public JTextArea text;

        public Calculator()
        {
            setSize(350,300); //sets size to 300 by 300 //LOOK UP HOW TO LOCK SIZE
            setResizable(false); //does not let user change the size of the window
            setDefaultCloseOperation(EXIT_ON_CLOSE);//makes app close when I press the x on the top left

            Container contentPane = getContentPane();//gets the contentPane
            contentPane.setBackground(Color.CYAN);//sets background color to white         

            contentPane.setLayout(new FlowLayout());//makes the contentPane read from left to right

            JTextArea text = new JTextArea(1, 25);
            contentPane.add(text);
            text.setEditable(false);

            button1 = new JButton ("7");
            contentPane.add(button1);
            button1.addActionListener(this);

            button2 = new JButton ("8");
            contentPane.add(button2);
            button2.addActionListener(this);

            button3 = new JButton ("9");
            contentPane.add(button3);
            button3.addActionListener(this);

            button4 = new JButton ("÷");
            contentPane.add(button4);
            button4.addActionListener(this);

            button5 = new JButton ("4");
            contentPane.add(button5);
            button5.addActionListener(this);

            button6 = new JButton ("5");
            contentPane.add(button6);
            button6.addActionListener(this);

            button7 = new JButton ("6");
            contentPane.add(button7);
            button7.addActionListener(this);

            button8 = new JButton ("x");
            contentPane.add(button8);
            button8.addActionListener(this);

            button9 = new JButton ("1");
            contentPane.add(button9);
            button9.addActionListener(this);

            button10 = new JButton ("2");
            contentPane.add(button10);
            button10.addActionListener(this);

            button11 = new JButton ("3");
            contentPane.add(button11);
            button11.addActionListener(this);

            button12 = new JButton ("-");
            contentPane.add(button12);
            button12.addActionListener(this);

            button13 = new JButton ("0");
            contentPane.add(button13);
            button13.addActionListener(this);

            button14 = new JButton (".");
            contentPane.add(button14);
            button14.addActionListener(this);

            button15 = new JButton ("=");
            contentPane.add(button15);
            button15.addActionListener(this);

            button16 = new JButton ("+");
            contentPane.add(button16);
            button16.addActionListener(this);



        }

        public static void main(String[] args)
        {
            Calculator guiWindow = new Calculator(); //uses GUI
            guiWindow.setVisible(true); //makes it visible

        }

        public void actionPerformed(ActionEvent e)
        {
            Container contentPane = getContentPane();

            if (e.getActionCommand().equals("7"));
            {
                text.setText("7");
            }






        }

    }

Solution

  • It looks like you shadowed your member field JTextArea text inside the constructor. So that means you didn't assign an instance of JTextArea to the member field but to the local variable in the constructor.

    Use instead of this

    JTextArea text = new JTextArea(1, 25);
    

    this

    text = new JTextArea(1, 25);