• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Enable JButton when 3 fields are filled in

 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JButton that I want to enable only when 3 specific fields (there are a many other fields) of a JDialog are filled in. What is the best way to do this? I'm thinking I need to check the value of each of those fields each time any one of them is updated but that seems kind of burdensome. Is there some other better way? TIA.
 
Marshal
Posts: 70654
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you add document listeners to the three fields? Trigger the action when any of them is changed, and myButton.setEnabled(!box1.getText().isempty() && ...)
Set a Timer which checks the three fields every second, 0.1second, or similar?
 
Rancher
Posts: 3208
29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm thinking I need to check the value of each of those fields each time any one of them is updated but that seems kind of burdensome



That is the easiest way to do it.

The trick is to create a simple API so that your solution scales whether you have 2 text fields or 20 text fields to check. In other words you don't do the check based on variable names.

You can create a class to register text field you want to check and store the text fields in a List. You add a DocumentListener to each text field. Then you simply iterate through the List whenever a DocumentEvent is generated.

 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You can create a class to register text field you want to check and store the text fields in a List. You add a DocumentListener to each text field.


I hadn't thought of that. I'll give it a try. Thanks.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic