dynamic creation of JTextField & getting the value from each field

Dear All,

I have a loop which creates JTextField and adds them to a JPanel.

for (Iterator experimentIt = statisticsType.iterator(); experimentIt.hasNext(); ){

String item = (String) experimentIt.next();

statsfilterspanel.add(new JTextField());

}

I cant seem to figure out a way to getText() from each of these JTextFields as their instance dont have a name.

Is there a way to dynamically create JTextFields and keep a track of which JTextField has what value?

Many thanks in advance.

Anuj.

[671 byte] By [agoela] at [2007-10-3 10:26:09]
# 1
Create an array of textFields and add the textfields in the loop to it. Later you can loop through the array to get the texts.
PhHeina at 2007-7-15 5:48:17 > top of Java-index,Desktop,Core GUI APIs...
# 2
Hi,Thanks for replying.I am not sure how many textfields I need and I am not interested in hardcoding a fixed number of textfields in an array.Is there another way?Thanks for your help.RegardsAnuj
agoela at 2007-7-15 5:48:17 > top of Java-index,Desktop,Core GUI APIs...
# 3
> I am not sure how many textfields I need and I am not> interested in hardcoding a fixed number of textfields> in an array.> > Is there another way?Yes, don't hardcode, do it dynamically.
zadoka at 2007-7-15 5:48:17 > top of Java-index,Desktop,Core GUI APIs...
# 4
You could use an ArrayList.-Puce
Pucea at 2007-7-15 5:48:17 > top of Java-index,Desktop,Core GUI APIs...
# 5
Hello,It will be nice if you can paste a sample code or redirect me to a relevant post in the forum to create new JTextFields dynamically. I tried looking for it and was not able to find something useful for me.Thanks much appreciated.RegardsAnuj
agoela at 2007-7-15 5:48:17 > top of Java-index,Desktop,Core GUI APIs...
# 6

> Hello,

>

> It will be nice if you can paste a sample code or

> redirect me to a relevant post in the forum to create

> new JTextFields dynamically.

>

> I tried looking for it and was not able to find

> something useful for me.

How where you planning to do it with a hardcoded value? Just replace the value with whatever the dynamic value would be.

zadoka at 2007-7-15 5:48:17 > top of Java-index,Desktop,Core GUI APIs...
# 7

// Make this a class variable to it can be accessed in any method

ArrayList textFields = new ArrayList();

...

for (Iterator experimentIt = statisticsType.iterator(); experimentIt.hasNext(); ) {

String item = (String) experimentIt.next();

//statsfilterspanel.add(new JTextField());

JTextField textField = new JTextField();

statsfilterpane.add( textField );

textFields.add( textField );

}

camickra at 2007-7-15 5:48:17 > top of Java-index,Desktop,Core GUI APIs...