This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes variable number of text fields Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "variable number of text fields" Watch "variable number of text fields" New topic
Author

variable number of text fields

chop ficaro
Ranch Hand

Joined: May 02, 2010
Posts: 30
i have a window with one text field in it that accepts an int, and as soon as a user enters that int i want a window to appear with the entered amount of text fields. and as soon as the user enters the data in those text fields, my program needs to store the data in those text fields in an array or vector or something so i can access it later

i know ill have to use a for loop with the entered number and create the textfields and add them to a panel, but how do i refer to them when i dont know how many there will be?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

chop ficaro wrote:i know ill have to use a for loop with the entered number and create the textfields and add them to a panel, but how do i refer to them when i dont know how many there will be?

You can add them in a List when you create them, and loop through that list.


[My Blog]
All roads lead to JavaRanch
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Christophe is right. You essentially turn a List<JTextField> into a List<String> by calling getText for each of the text fields. Perhaps you can switch to using JTextField[] and String[] instead as the size never changes once created.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
GeeCON Prague 2014
 
subject: variable number of text fields