9

So as you may know, if you have a text field and you add an ActionListener to it, it will only listen to the keypress of the enter button. However, I want to let my ActionListener listen to changes in text of the . So basically I've got this:

    public static JPanel mainPanel() { 
    JPanel mainp = new JPanel(); 
    JTextArea areap = new JTextArea("Some text in the textarea"); 
    JTextField fieldp = new JTextField("Edit this"); 
    areap.setEditable(false); 
    fieldp.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
             if(//change in textfield, for instance a letterpress or space bar)
                   { 
                        //Do this
                   } 
        }
    });
    mainp.add(areap);
    mainp.add(fieldp); 
    return mainp;
}

Any way I can listen to changes in text (like documented in the actionPerformed event)?

kleopatra
  • 51,061
  • 28
  • 99
  • 211
ZimZim
  • 3,291
  • 10
  • 49
  • 67
  • 1
    possible duplicate of [swing: appropriate Listener for JTextField change events](http://stackoverflow.com/questions/2165071/swing-appropriate-listener-for-jtextfield-change-events) – duffymo Dec 05 '11 at 10:36

3 Answers3

21

From an answer by @JRL


Use the underlying document:

myTextField.getDocument().addDocumentListener();
Community
  • 1
  • 1
COD3BOY
  • 11,964
  • 1
  • 38
  • 56
2

Yeah, but what is a document listener and how do you use it? You're not really answering the question.

I have a JTextField in my app's user interface. When the user makes any change to it, I want a nearby JCheckBox to be checked. The purpose is to tell the app to USE the value that was entered. Users often enter a value there but if they don't explicitly tell the app to use it then the app continues to ignore it. Instead of "training" users I'm supposed to follow the principle of least astonishment and automatically check the "Use this value" box.

But how do I listen for a change? Can't you guys just tell me the easy way, instead of "educating me" about document listeners?

Ed Poor
  • 1,849
  • 20
  • 13
  • When I asked this question I had implied that I knew how an ActionListener worked, therefore I should also known how (to figure out how) a DocumentListener works. The answer was more than enough. You add the DocumentListener to the JTextField's document as mentioned, and you can check the API Javadocs for what functionality that specific listener has. – ZimZim Dec 02 '13 at 18:21
  • maybe you can get an idea from this http://www.coderanch.com/t/610054/GUI/java/Add-DocumentListener-validate-multiple-JTextFields – Alex Burdusel Jan 24 '14 at 15:57
0

Documents are the mechanisms java swing uses to store the text inside of a JTextField. DocumentListeners are objects that implement the DocumentListener interface and thus make it possible for you to list to changes in the document, i.e. changes in the text of the JTextField.

To use the document and documentlistener capabilities, as suggested above extend your class (probably but not necessarily a JFrame) so that it implements the DocumentListener interface. Implement all the methods for the interface (most likely your java ide can do that semi-automatically for you. FYI, the DocumentListener interface has three methods, one for inserting characters (into the text field), one for removing characters, and one for changing attributes. You are going to want to implement the first two as they are called when characters are added (the first one) or deleted (the second one). To get the changed text, you can either ask the document for the text, or more simply call myTextField.getText().

C'est tout!

Phil Troy

user2383235
  • 129
  • 1
  • 2
  • Please See this first: [answering](https://stackoverflow.com/help/answering) and [how-to-answer] (https://stackoverflow.com/help/how-to-answer) – always-a-learner Jun 30 '17 at 03:36