Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTextField listener to enable button action

 
K. Tsang
Bartender
Posts: 3457
14
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, I have this minor yet desired issue. I have a dialog window with a form. The form has some text fields and 2 buttons submit and cancel. I want to disable the submit button by default (using actions) and when all the text fields are entered (not empty) then the submit button becomes activated.

I don't really know what kind of listeners to use to achieve this effect. I'm able to do the same thing but with radio buttons and such but not with text fields. Any ideas? Thanks.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, perhaps a DocumentListener added to all of the JTextFields that check all of them for valid input and then enables the button (or if missing, disables the button).

edit: I meant adding a DocumentListener to each of the JTextField's Documents. For instance:
 
K. Tsang
Bartender
Posts: 3457
14
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks pete, document listener is just what i'm looking for. Now I can use this instead of my previous key listener to check valid input too.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:thanks pete, document listener is just what i'm looking for. Now I can use this instead of my previous key listener to check valid input too.

You're quite welcome. This way you can also trap mouse -initiated text pastes and cuts to the JTextField, all that would have bearing on your button's state.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic