user724198
user724198

Reputation:

Java/Netbeans -- Reference a jTextfield dynamically?

Not sure if this is even remotely possible, but...

I have a jFrame with 10 different Textfields. Elequently named tf1, tf2, tf3...

What I'd LOVE to do is be able to reference them dynamically. Something like:

int i = 1;

while (i<11) {
    tf[i].settext("blah - " + i);
}

Any ideas? If anybody knows of a working example it'd be great.

Upvotes: 0

Views: 133

Answers (1)

WillShackleford
WillShackleford

Reputation: 7008

If you want to set the text of all the textfields in a JFrame:

Component ca[] = getContentPane().getComponents();
System.out.println("ca = " + Arrays.toString(ca));
int i = 0;
for(Component c: ca) {
    if(JTextField.class.isAssignableFrom(c.getClass())) {
        JTextField tf = (JTextField) c;
        tf.setText("blah -"+(++i));
    }
}

Upvotes: 1

Related Questions