File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JTextField listener to enable button action Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextField listener to enable button action" Watch "JTextField listener to enable button action" New topic
Author

JTextField listener to enable button action

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2584
    
    9

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.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Sep 13, 2007
Posts: 2584
    
    9

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

Joined: Feb 23, 2007
Posts: 1561
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTextField listener to enable button action