Text Changed event in JTextArea? How to?

前端 未结 3 706
眼角桃花
眼角桃花 2020-12-03 17:05

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

相关标签:
3条回答
  • 2020-12-03 17:19

    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) {
    
            }
        });
    
    0 讨论(0)
  • 2020-12-03 17:29

    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?

    0 讨论(0)
  • 2020-12-03 17:44

    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.

    0 讨论(0)
提交回复
热议问题