Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I fill JTextFields (setText) in a loop using getComponent()

449 views
Skip to first unread message

wer...@my-deja.com

unread,
Jun 26, 2000, 3:00:00 AM6/26/00
to
I'm trying to fill a bunch of JTextField's with data from a database.
The JTextFields have already been instantiated 1-26. How do I do
setText in a loop to fill these up with data without doing a case on
each one.


Here is the code which I'm trying to incorporate, but I don't know how
to pass the data into the JTextField as the getComponent method does
not seem to be that specific:

public void fillTextArea() throws SQLException
{
getSetDatafromDB gsdfdb = new getSetDatafromDB();
int counter = 1;
for (int column = 0; column < this.getContentPane
().getComponentCount(); column++)
{
String name = getContentPane().getComponent
(column).getClass().getName();
if(name.equals("javax.swing.JTextField"))
{

Object temp = "JTextField" + counter + ".setText(" +
gsdfdb.getValueAt(counter-1).toString() + ")";
//javax.swing.JTextField(gsdfdb.getValueAt
(column).toString());
//getContentPane().getComponent(column).getClass
(). .setText(gsdfdb.getValueAt(column).toString());
System.out.println(temp);
System.out.println(column + " column");
System.out.println(counter + " counter");
counter++;
}

}


Sent via Deja.com http://www.deja.com/
Before you buy.

Ben Z. Tels

unread,
Jun 27, 2000, 3:00:00 AM6/27/00
to
> I'm trying to fill a bunch of JTextField's with data from a database.
> The JTextFields have already been instantiated 1-26.

And you named these variables JTextField1, JTextField2,...,JTextField26
right? Like you would do in JavaScript?
That won't work in Java. Java, being a real language, doesn't treat source
code like text you can edit on-the-fly.

> How do I do
> setText in a loop to fill these up with data without doing a case on
> each one.

The normal way to do it is to create an array of JTextFields and loop
through that:

JTextField[] jtfarr = new JTextField[26];
for (int i = 0; i < 26; i++)
{ jarr[i] = new JTextField();
jarr[i].setText(....);
}

> String name = getContentPane().getComponent
> (column).getClass().getName();
> if(name.equals("javax.swing.JTextField"))
> {

There's a very useful shorthand notation in java for this (it's not
beutiful, but still):

if (getContentPane().getComponent(column) instanceof javax.swing.JTextField)

> Object temp = "JTextField" + counter + ".setText(" +
> gsdfdb.getValueAt(counter-1).toString() + ")";

This would probably work in JavaScript. Not in a real programming language
though.


--
Ben Z. Tels
opti...@stack.nl
http://www.stack.nl/~optimusb/
UIN:2474460

"The Earth is the cradle of the mind, but one cannot stay in the cradle
forever."
--Tsiolkovsky

jo...@wellfound.com

unread,
Jun 27, 2000, 3:00:00 AM6/27/00
to
Ben,

Someone else mentioned using an array as well. The only problem with
this is that I would then have to setBounds and all that as well in the
array or do a case, unless I'm overlooking something. I've created the
Textfields in Visual Cafe and like the look and feel where they are.
What I want to figure out is how to use the setText method in a loop
not knowing what the instantiation of the JTextField is.

In article <8jacqa$6q5$2...@news.IAEhv.nl>,

Ben Z. Tels

unread,
Jun 27, 2000, 3:00:00 AM6/27/00
to
> Someone else mentioned using an array as well. The only problem with
> this is that I would then have to setBounds and all that as well in the
> array or do a case, unless I'm overlooking something.

Can't say in general; it depends on what you're trying to do and I don't
know what you're trying to do.

> I've created the
> Textfields in Visual Cafe and like the look and feel where they are.
> What I want to figure out is how to use the setText method in a loop
> not knowing what the instantiation of the JTextField is.

Well, you can ask the Container that contains the JTextFields for an array
containing the java.awt.Components in the Container ( getComponents() ) and
then cycle through that with a loop. Usually, components are placed in the
array in the order in which they were added to the container, but there's no
hard guarantee of that. You'd probably still end up with a large selection
statement to fnd out exactly which component is which.

There's no way to make a selection in a loop based on the variable's name
(you can use the variable, but that won't generalize to a loop). If for no
other reason, the variable name doesn't get compiled into the bytecode.

0 new messages