3

I have my class and I have implemented DocumentListener

public void removeUpdate( DocumentEvent arg0 ) {
   System.out.println( arg0.getDocument());
}

It would print javax.swing.text.PlainDocument@49ea903c

Is there any possible way I would get the object so I can get the value of the changed textfield? At the moment I have only one field so I do not need a check, but what if I use two or more, how do I know which JTextField has notified the listener?

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
Nikola
  • 2,093
  • 3
  • 22
  • 43

5 Answers5

5

I'm not sure it's possible to get the swing component from a Document. But the issue is easily solved: just add a different instance of the listener to every text field, and store the text field in the listener itself.

textField1.getDocument().addDocumentListener(new MyDocumentListener(textField1));
textField2.getDocument().addDocumentListener(new MyDocumentListener(textField2));
textField3.getDocument().addDocumentListener(new MyDocumentListener(textField3));
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
3

One option is to use an inner class, which will provide you an opportunity to reference the text field.

final JTextField field = new JTextField();

field.getDocument().addDocumentListener(new DocumentListener() {
  // Here you can reference 'field' in your methods
});

If you need to perform the same action for each text field, JB Nizet's solution will be neater.

Community
  • 1
  • 1
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
0

You actually are getting the PlainDocument object. Just store it in a variable instead of printing it.

For more info see docs.

Mateusz
  • 3,038
  • 4
  • 27
  • 41
0

What you need to do is adding document listners to each component you need.If you really need to know which text fields text has changed then you can have a property in the DocumentListner as textFieldName or something and you can set it when you create the document listner for them.But I think you better change your approaching to situation.It doesn't sound good.

Sanjaya Liyanage
  • 4,706
  • 9
  • 36
  • 50
0

You may update the document without knowing the context.

Consult the documentation of javax.swing.event.DocumentEvent

These methods combined with the document retrieved by javax.swing.event.DocumentEvent.getDocument() allow you to update the document in a proper way. Later you may add other text field without any change.

To simply get the text of a JTextField after change:

JTextField myTf = new ...; // maybe an attribute definition
...
myTf.addActionListener(
   new ActionListener(){@Override public void actionPerformed( ActionEvent e ) {
      JTextField tf = (JTextField)e.getSource();
      System.out.println( tf.getText());
      if( tf == myTf ) { // == for reference comparison
         ... // do something dedicated to myTf
      }
   }});
Aubin
  • 14,617
  • 9
  • 61
  • 84
  • I have gone through the documentation several times before I have asked my question here, but I do not see how getOffset() or getLenght() is helping me. I want to get the text in the field after the change, not the length as I will do a search based on that – Nikola May 18 '13 at 16:18
  • DocumentListener is used for more complex action, see my edit – Aubin May 18 '13 at 17:57