Results 1 to 7 of 7
  1. #1
    Muskar is offline Member
    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    0

    Default DocumentListener, KeyListener or a third option?

    Hey.

    I have a few JTextFields, currently with documentListeners.

    Now, what I want to do is basically to check, inside the documentListener-methods, whether the input to the JTextField is done by the user, or by the code (via .setText(String)).

    Should I just use the KeyListener instead or is there a third option that I'm not familiar with?

    Thanks in advance!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Key listener won't work if you paste in text. A DocumentFilter would but I don't think it could distinguish between text from the app vs. the user. You could I suppose set a flag when the app changes the text and then unset the flag after the text has been changed.

  3. #3
    Muskar is offline Member
    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Key listener won't work if you paste in text. A DocumentFilter would but I don't think it could distinguish between text from the app vs. the user. You could I suppose set a flag when the app changes the text and then unset the flag after the text has been changed.
    I was thinking of removing the documentListener and adding it again - but I have no idea if it will create flaws that I don't know of.

    Since I'm not familiar with flags I'd very much appreciate further explanation or even better: a short example with a short explanation included.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I refer to having a boolean variable in the class that is set to true or false, nothing more.

    Java Code:
    private boolean appUpdatingText = false;
    
    
    private void appUpdateField(JTextField textField, String text) {
      appUpdatingText = true;
      textField.setText(text);
    }
    
    
    //... then in the listener code
    if (appUpdatingText) {
      appUpdatingText = false;
    } else {
      //... do what you want to do if the user updates the field
    }
    Last edited by Fubarable; 11-20-2010 at 11:18 PM.

  5. #5
    Muskar is offline Member
    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I refer to having a boolean variable in the class that is set to true or false, nothing more.
    I have learned it as "states", opposed to what you refer to as "flag" - but thanks a lot - I cannot believe I didn't think of that myself haha :) !

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Muskar View Post
    I have learned it as "states", opposed to what you refer to as "flag" - but thanks a lot - I cannot believe I didn't think of that myself haha :) !
    I may be wrong but I think of state as a more general term -- the values of the object's key fields, and flag as a specific term for the state of a boolean.

  7. #7
    Muskar is offline Member
    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I may be wrong but I think of state as a more general term -- the values of the object's key fields, and flag as a specific term for the state of a boolean.
    Don't get me wrong - My teacher only just mentioned 'states' last Friday - so I'm not pretending to be an expert.
    Your explanation of the expression 'flag' sounds plausible enough, so you're probably right.

Similar Threads

  1. Replies: 3
    Last Post: 05-28-2010, 04:18 AM
  2. JAX-WS\METRO - apt with option option key="r"
    By Talkinjive in forum Web Frameworks
    Replies: 0
    Last Post: 01-31-2010, 04:15 PM
  3. KeyListener - Is this what I need?
    By dbashby in forum New To Java
    Replies: 26
    Last Post: 04-18-2009, 04:14 PM
  4. KeyListener Example
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:46 PM
  5. how to add a KeyListener
    By leonard in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 04:44 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •