The goal of this program is pretty modest:
There's a TextField, and a TextListener attached. So everytime the TextField is edited at all, textValueChanged(TextEvent e) method is triggered. This works fine.
What I need to do, is every time the event is triggered, I need to compare the text in the TextField to a list of keywords. However, for some reason, if the text in the TextField is "a", (TextField.getText() == "a") returns false! Why!!!
Here's my program, with an example of the issue:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeEvent; import java.text.*; public class FormattedTextFieldDemo extends JPanel implements TextListener { private TextField textField; public FormattedTextFieldDemo() { super(new BorderLayout()); textField = new TextField(10); textField.addTextListener(this); JPanel labelPane = new JPanel(new FlowLayout()); labelPane.add(textField); add(labelPane, BorderLayout.NORTH); } /** * THIS IS THE PROBLEM. **/ public void textValueChanged(TextEvent e) { String currentText = textField.getText().toString(); if (currentText == "a" || currentText == "a ") { System.out.print("it's a!"); } else { System.out.print("it's not a."); } } private static void createAndShowGUI() { JFrame frame = new JFrame("Austin's Program"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new FormattedTextFieldDemo()); frame.setSize(new Dimension(200,200)); frame.setVisible(true); } public static void main(String[] args) { createAndShowGUI(); } }