JTextField: can't listen when the text is changed

Discussion in 'Java' started by Federico, Aug 30, 2004.

  1. Federico

    Federico Guest

    Hello,

    I'm fighting with a JTextField and don't find how to call getText
    everytime the text is changed. I tried the code shown below
    (automatically generated by NetBeans) but it does nothing (never prints
    "Hola").

    I tried to add a KeyListener for keyTyped and it works but when I call
    getText the last key typed is not there.

    Do you know what happens?
    (I'm working on a Mac OSX with java 1.4.2_05)

    Thanks in advance,
    Federico



    jTextField1.addInputMethodListener(new
    java.awt.event.InputMethodListener() {
    public void
    inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
    jTextField1InputMethodTextChanged(evt);
    }
    public void
    caretPositionChanged(java.awt.event.InputMethodEvent evt) {
    jTextField1CaretPositionChanged(evt);
    }
    });

    private void jTextField1InputMethodTextChanged(
    java.awt.event.InputMethodEvent evt) {
    // TODO add your handling code here:
    System.err.println("Hola");
    }
     
    Federico, Aug 30, 2004
    #1
    1. Advertising

  2. Federico

    Federico Guest

    I found the solution in an old message:


    Autor:Mark McMillan ()
    Asunto:Re: Event when text is changed in TextField
    Fecha:2000/06/13

    You want to listen for changes on the Document that is associated
    with the TextField. If you don't explicitly create a Document the
    TextField does it for you. To setup a listener:

    // Listen for changes in the text
    myTextField.getDocument().addDocumentListener(new DocumentListener() {
    public void changedUpdate(DocumentEvent e) {
    // text was changed
    }
    public void removeUpdate(DocumentEvent e) {
    // text was deleted
    }
    public void insertUpdate(DocumentEvent e) {
    // text was inserted
    }
    });

    Note this works no matter how the text gets changed; via a
    clipboard cut/paste, progamatic "setText()" on the TextField,
    or the user typing into the field on the UI.

    Hope this helps,
    -Mark McMillan




    Federico wrote:

    >
    > Hello,
    >
    > I'm fighting with a JTextField and don't find how to call getText
    > everytime the text is changed. I tried the code shown below
    > (automatically generated by NetBeans) but it does nothing (never prints
    > "Hola").
    >
    > I tried to add a KeyListener for keyTyped and it works but when I call
    > getText the last key typed is not there.
    >
    > Do you know what happens?
    > (I'm working on a Mac OSX with java 1.4.2_05)
    >
    > Thanks in advance,
    > Federico
    >
    >
    >
    > jTextField1.addInputMethodListener(new
    > java.awt.event.InputMethodListener() {
    > public void
    > inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
    > jTextField1InputMethodTextChanged(evt);
    > }
    > public void
    > caretPositionChanged(java.awt.event.InputMethodEvent evt) {
    > jTextField1CaretPositionChanged(evt);
    > }
    > });
    >
    > private void jTextField1InputMethodTextChanged(
    > java.awt.event.InputMethodEvent evt) {
    > // TODO add your handling code here:
    > System.err.println("Hola");
    > }
     
    Federico, Aug 30, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    689
    Rosanne
    Oct 11, 2005
  2. =?ISO-8859-1?Q?Peter_Valdemar_M=F8rch?=
    Replies:
    2
    Views:
    858
    =?ISO-8859-1?Q?Peter_Valdemar_M=F8rch?=
    Jul 23, 2003
  3. Lovely Dola
    Replies:
    3
    Views:
    15,210
    Christian Kaufhold
    Dec 18, 2003
  4. Donny
    Replies:
    13
    Views:
    671
    Donny
    Aug 5, 2004
  5. kernelerror
    Replies:
    2
    Views:
    643
    Andrew Thompson
    Jun 16, 2008
Loading...

Share This Page