Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

TextListener in JTextField ???  RSS feed

 
v fox
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello -
I am looking for a TextListener for JTextField. I know that TextListener was available for the AWT TextField component, however, I cannot find the same listener in Swing. How can I have an event triggered whenever the text value changes? This is a requirement for my application. Many thanks in advance for your help.
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are looking for a InputMethodListener.
The interface requires two routines:
public void caretPositionChanged(InputMethodEvent ev){}
public void inputMethodTextChanged(InputMethodEvent ev){}
Manfred.
 
v fox
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me make sure I understand: you are saying that I can add an InputMethodListener to my textfield and then implement inputMethodTextChanged(). I can check the source of the event, which will notify me which textfield had changed value. In doing so, this listener will notify me only when the text value has changed, not when the textfield has only received focus or the caret position has changed. Does it fire once for each change? For example, if the original value is dog, and the user wants to change it to cat, does it fire 4 times if the 1)user deletes the original value, 2)types 'c', 3)then 'a', 4)then 't'? I just need to know when the user is finished changing the value. Is this possible?
Thank you for your response!
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about that from your original question I assumed you wanted to know about every character. Your assumption is correct in the number of times it will fire an event.
Your problem is much simpler if you just want the end result after the user has completed entering text. The TextField uses an ActionItemListener to handle that event. The following will work:
yourTextField.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// Perform your work here ...
}
});
Sorry about the confusion ...
Manfred.
 
v fox
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am aware of ActionListener; however I thought that event was only fired if the user hits enter. In which case, the user probably will not be hitting enter after changing a value, but rather tabbing on to the next field that he needs to change. I need to know when the text has finished being changed, such as on lost focus. If the user is tabbing through my form and changes a value that is programmatically set, I need to be made aware of this. However, if the user is just tabbing through field x to get to field y and in the process of doing so, he does not change the contents of field x, do not notify me. What is the best method for doing this?
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately I know of no way to perform your exact case (i.e., firing focusEvent only when text changes). The FocusEvent will be fired regardless of text changing because it is a low-level event.
It would be up to your FocusListener to decide whether the text has indeed changed or if the user was just tabbing around.
Sorry,
Manfred.
 
eric moon
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get so tired of these users that are always just "Tabbing Around", making things hard for those trying to make an honest living! ;^)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!