Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How do i detect am empty text field condition?

843804Oct 13 2004 — edited Oct 15 2004
Hi im a newbie with swing. I have a small problem. I have a text field. When user types some text into it i display a pane in the GUI. When the text field is cleared, i need to hide the pane. So basically i need to detect the condition when the text field is empty.
I tried one approach in which i capture key press event. So when user presses the backspace key to clear the text field, the event is fired. In the keyPress method, i check if text field is empty using getText() method.
So my keyPressed method is as follows:
keyPressed()
{
if(textField.getText().trim().equals(""))
{
// make my pane invisible here
}
}
This does work , however it requires one additional key press before detecting that the text field is empty. For ex if there are 3 letters in the text field, i require to press the backspace key 4 times to hide the pane. I think this is because the key presses event occurs first & is handled & after that the GUI is repainted with the new data.
Any idea how i can tackle this? Or is there any other way in which i can detect the moment the text field becomes empty?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 12 2004
Added on Oct 13 2004
8 comments
1,908 views