Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextField event  RSS feed

 
Nawar Gailani
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
I have JTextField and JButton. If i want the JButton to be enabled or disabled according to if there is something in the JTextField, what event i should use? I used this code:

but myButton is always disabled whatever i enter in myText.

Thanks in advance
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would use a DocumentListener:

field.getDocument().addDocumentListener(myDocumentListener);
 
Nawar Gailani
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeff, I'll try it
 
Nawar Gailani
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I tried the DocumentListener but I had only the half functionality.
I mean when the program start the button is disabled, and when I write something in the text field, it enables the button. This is great, but when I delete everything inside the text field, the button supposed to be disabled, but it doesn't. I don't know what is missing in my code.
Here is the inner class that implements DocumentListener:



and in the constructor of the form:



In Sun tutorial, it said changeUpdate(DocumentEvent e) doesn't apply on text field, so is this the reason that it doesn't update? Or something wrong or missing with the code?

I appreciate any help or opinion.
 
Anand Loni
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using e.getLength() which will return u number of changes in document, but u want length of document.
So use doc.getLength() instead of e.getLength().
 
Nawar Gailani
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Anand
This did it.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!