aspose file tools
The moose likes Swing / AWT / SWT / JFace and the fly likes Looping through input controls Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT / JFace
Reply Bookmark "Looping through input controls" Watch "Looping through input controls" New topic
Author

Looping through input controls

George Hutto
Greenhorn

Joined: Mar 12, 2003
Posts: 8
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
Rancher

Joined: Jun 09, 2003
Posts: 4041
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

Joined: Mar 12, 2003
Posts: 8
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
Rancher

Joined: Jun 09, 2003
Posts: 4041
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
Bartender

Joined: Oct 14, 2005
Posts: 13842

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.
 
 
subject: Looping through input controls
 
Threads others viewed
for statements
Dan's Exam on Flow control
Display or not to display other controls
Passing array of checkboxes from one JSP to another
Adding/Deleting children from collection w/o fetching the whole collection
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com