Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating TextField through loops

Status
Not open for further replies.

dhaywood

IS-IT--Management
Jun 25, 2002
51
0
0
GB
Is there any way to create TextField through a loop ie

int i = 1;
String code = "Code" + 1;
JTextField code = new JTextField;

so if this was in a loop each time around the loop i would increase and you would get a TextField box called code1 code2 code3 ect...

Is there any way to do this.

Thanks
 
This would be one way to do what you suggest :

Code:
int numberOfFields = 6;
JTextField[] tfs = new JTextField[numberOfFields];

for (int j = 0; j < tfs.length; j++) {
  tfs[j] = new JTextField();
}

The compiler would barf if you attempted the way you sugeested, as &quot;code&quot; would be seen as a String object, not a JTextField object name.
 
That seems to be a pretty good way of doing it, thanks I will give it a go but I am sure it will work. Thanks Again
 
The &quot;barf&quot; as you put it did happen, if I was to use the code you suggested, would I be able to use getText() to grab the text out of the textfield created.

Would it be tfs[1].getText() or something else.

Thanks
 
tfs[1].getText() - thats the way !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top