30

I have been trying to make a text changed event handling mechanism for my JTextArea. For my purposes an event has to be fired whenever there is a change in the text of the JTextArea. I tried using the KeyListener interface and here is my code for that.

txtArea.addKeyListener(new KeyListener() {

    @Override
    public void keyTyped(KeyEvent arg0) {

    }

    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyPressed(KeyEvent arg0) {
        currentText = text.getText();
        if (currentText == textString)
            JOptionPane.showMessageDialog(null, "Correct");

    }
});

Nothing happened when the textarea's text matched the hardcoded text. How can an event changed event be made for this.
Can this objective be achieved with a PropertyChangedListener? If it can, then how?

prometheuspk
  • 3,754
  • 11
  • 43
  • 58
  • See this question: [**Value Change Listener to JTextField**](https://stackoverflow.com/questions/3953208/value-change-listener-to-jtextfield). – Boann Nov 28 '14 at 14:01

3 Answers3

48

I would get the JTextArea's Document via getDocument() (a PlainDocument actually) and use a DocumentListener to listen for changes. This way you'd capture changes from key strokes and also from copy/paste/cut events.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    @Prometheus87: thanks for the acceptance. We all answered correctly and at about the same time. So given the above, I wouldn't mind it if you "unaccepted" me and and accepted either of the other two answerers since I don't need the points, if you know what I mean. 1+ to you (and the others). – Hovercraft Full Of Eels Oct 12 '11 at 16:20
  • 1
    @hovercraft-full-of-eels thank you man. Can i contact you on any IM service or Social Network, you seem very resourceful regarding Java. – prometheuspk Oct 12 '11 at 16:23
  • 14
    No, I'm kind of busy. Best for you to throw your questions here on SO. Luck to you. – Hovercraft Full Of Eels Oct 12 '11 at 16:30
44

Not the JTextArea, but the contained document receives updates, so you need:

jTextArea.getDocument().addDocumentListener(new DocumentListener() {

        @Override
        public void removeUpdate(DocumentEvent e) {

        }

        @Override
        public void insertUpdate(DocumentEvent e) {

        }

        @Override
        public void changedUpdate(DocumentEvent arg0) {

        }
    });
aleroot
  • 71,077
  • 30
  • 176
  • 213
NCode
  • 2,758
  • 1
  • 16
  • 25
  • 1
    two things: 'changedUpdate' does not fire for "plain text components" (see Oracle's docs http://docs.oracle.com/javase/tutorial/uiswing/events/documentlistener.html ) and its argument should be 'e', not 'arg0'. – marklark Jan 15 '16 at 23:28
  • 1
    and how shouldn't we check the jtextarea.getText() from that listener? – gumuruh Feb 02 '17 at 01:07
17

You are comparing strings with ==

if (currentText == textString)

This will never be true. This compares if the strings are the same String object. You should use equals.

if (currentText.equals( textString) )

You might also want to check out DocumentListeners. See also this How do I compare strings in Java?

Community
  • 1
  • 1
Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67