Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Simple TextListener Problem =/ Please help!

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Simple TextListener Problem =/ Please help!

    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();
        }
    }


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Simple TextListener Problem =/ Please help!

    Reading: Java: String Comparison

    Chris

  3. The Following User Says Thank You to Freaky Chris For This Useful Post:

    austin.rose94 (December 8th, 2011)

  4. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple TextListener Problem =/ Please help!

    It works! Thank you so much. I was wondering if it had something to do with deep vs shallow equality. Too new with Java to know much about it though.

Similar Threads

  1. Simple game problem
    By frozen java in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 30th, 2011, 09:14 PM
  2. please help, simple problem
    By grandmst in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 13th, 2011, 06:26 PM
  3. Need help with a simple jsp and servlet problem
    By javacoder_APAC in forum Java Servlet
    Replies: 4
    Last Post: March 23rd, 2011, 08:32 AM
  4. Very simple problem...PLEASE HELP!
    By dungeondragon in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 1st, 2011, 07:19 AM
  5. Simple problem...
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 6th, 2011, 12:02 AM