0

I'm pretty new to java and have been working on learning how GUI's work. Through some tutorials, I managed to piece together a program to use a text box with the hope to incorporate a calculator I made as my first project. The area I'm focusing on is the button and action listener, where I'm trying to get an extra response when typing in "calc".

I've tried initializing the extra response outside of the program, but because of the string being called inside, I can't get it to work. I also tried calling the string outside but that still didn't work. I have an idea doing anything past this will require separately called functions, but I was hoping to start by figuring out this smaller part first.

    static JTextField tf;
    static JFrame frame;
    static JPanel panel;
    static JTextArea ta;
    int count;
    int num1;
    int num2;
    int exp;
    char operator;
    double answer;

    static void GUI() {
        frame = new JFrame("Thank you for reading this");
        panel = new JPanel();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,400);
        JButton button = new JButton("Test");
        tf = new JTextField(15);
        panel.add(tf);
        panel.add(button);

        JTextArea ta = new JTextArea();
        ta.setEditable(false);

        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                String text = tf.getText();
                ta.append(text+"\n");
                if(text == "q") {
                    ta.append("something random\n");
                }
            }
        });

        frame.getContentPane().add(BorderLayout.CENTER, ta);
        frame.getContentPane().add(BorderLayout.SOUTH, panel);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        GUI();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }

}

I'm expecting to see "something random" appear below "calc" when it is typed into the text field. So far I receive nothing with the if statement

Frakcool
  • 10,915
  • 9
  • 50
  • 89
CJT157
  • 1
  • 1
  • Well, first things first: 1) Don't use `static` components. 2) See [How do I compare Strings in Java], 3) What's with the empty `actionPerformed` below? – Frakcool May 31 '19 at 20:14
  • Try text.equals(“q”) as equality between string is tricky – Sergiu May 31 '19 at 20:14

2 Answers2

1

Firstly, == Operator matches the reference of Strings or Objects not the actual value. String is not a primitive data type so that you can compare its value by using ==. You need to call equals() method for this:

if(text.equals("q")) {
      ta.append("something random\n");
}
Mustahsan
  • 3,852
  • 1
  • 18
  • 34
1

When comparing objects for value equality use equals instead of ==, because == is checking reference equality.

So instead of text == "q" write text.equals("q");

FilipRistic
  • 2,661
  • 4
  • 22
  • 31