Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Looping through input controls  RSS feed

 
George Hutto
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

This is a newbish question but I'd like to be able to loop through the textboxes, etc on my page and either disable/enable them based on a condition and/or change the background color.

What "collection" of controls do I loop through or where do I look for more information? I've done this in other languages but in Java I'm drawing a blank.

Thanks,

George
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
each container/parent has a getComponents() which you can iterate looking for instanceOf's,
or you can add the current textfields to an array, and iterate that array
 
George Hutto
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:each container/parent has a getComponents() which you can iterate looking for instanceOf's,
or you can add the current textfields to an array, and iterate that array


Michael, thanks for the quick response.

I'm looping through the components and testing instanceof JTextField, etc. Oddly, the components don't seem to have any sort of name I can retrieve. yes, I need to treat some text fields one way and some other text fields another way. Any quick sugggestion on this? Am I stuck using a setName() on each textfield so I have a name to pull?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
alternatively, you can create your own "collection" of controls

see if this thread from the last couple of days makes sense,
then you can access the textfields via their array index number.

http://www.coderanch.com/t/565820/GUI/java/Accessing-jLabel-reference
 
Paul Clapham
Sheriff
Posts: 22502
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would agree with Michael's suggestion: just keep track of the components you want to work with. Put them in an array or a List or something like that. Much easier than just throwing them into the GUI and then trying to rummage through the GUI and find where they went.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!