This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Could you make dynmically textfields, labels, and buttons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Could you make dynmically textfields, labels, and buttons" Watch "Could you make dynmically textfields, labels, and buttons" New topic
Author

Could you make dynmically textfields, labels, and buttons

bobby, morkos
Ranch Hand

Joined: Jan 04, 2002
Posts: 82
If I have a query that returns some values, I want to display it within labels, textfields, and buttons.
e.g. query that returns 5 recordset, then I want to have 5 labels, textfields, and buttons.

Another e.g. . query that returns 2 recordset, then I want to have 2 labels, textfields, and buttons.
lavanya subramanian
Ranch Hand

Joined: Nov 03, 2000
Posts: 52
hi bobby,
i'm not sure of what you are asking,see if this helps you,
yes we can create dynamic labels,textfields and buttons.
while(resultset.next())
{
TextFeild t = new TextField();
Label l = new Label();
Button b = new Button();
}
but if you are using the got out values to do some work then better get the number of components and declare those as label[],textfield[]and button[] arrays and initialize.

hope this would help u,
lavanya
bobby, morkos
Ranch Hand

Joined: Jan 04, 2002
Posts: 82
Is this what you mean, by the way I can't seem to get the idea, please help. Thanks.
JPanel pnlMain = new JPanel();
TextField t[] = new TextField();
Label l[] = new Label();
Button b[] = new Button();
for (int i=0; i < 4; i++){
btnName[i].setText(">> Name " + i);
pnlMain.add(btnName[i]);
}
Originally posted by lavanya subramanian:
hi bobby,
i'm not sure of what you are asking,see if this helps you,
yes we can create dynamic labels,textfields and buttons.
while(resultset.next())
{
TextFeild t = new TextField();
Label l = new Label();
Button b = new Button();
}
but if you are using the got out values to do some work then better get the number of components and declare those as label[],textfield[]and button[] arrays and initialize.

hope this would help u,
lavanya
lavanya subramanian
Ranch Hand

Joined: Nov 03, 2000
Posts: 52
hi bobby,

JPanel pnlMain = new JPanel();
TextField t[] = new TextField();
Label l[] = new Label();
Button b[] = new Button();//change here to declare the array to new Button[4];
for (int i=0; i < 4; i++){
b[i]=new Button("Name'+i);
pnlMain.add(b[i]);
}

i think this is what u asked for
lavanya
bobby, morkos
Ranch Hand

Joined: Jan 04, 2002
Posts: 82
I'm having difficulty with textfields, I have done something like show below and it compiled, but I get an error in runtime. Could you tell me what I need wrong with textfields. I'm trying everything and I can't figure it out. Please help?
JPanel pnlMain = new JPanel();
TextField t[] = new TextField[4];
Label l[] = new Label();
Button b[] = new Button();//change here to declare the array to new Button[4];
for (int i=0; i < 4; i++){
b[i]=new Button("Name" + i);
t[i].setText("text " + i);
pnlMain.add(b[i]);
}
Originally posted by lavanya subramanian:
hi bobby,

JPanel pnlMain = new JPanel();
TextField t[] = new TextField();
Label l[] = new Label();
Button b[] = new Button();//change here to declare the array to new Button[4];
for (int i=0; i < 4; i++){
b[i]=new Button("Name'+i);
pnlMain.add(b[i]);
}

i think this is what u asked for
lavanya
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

You are confusing an array of objects with the objects inside it...

This declares an array of Textfields:


This instantiates the array:


And this instantites an object in the array:


So, to create an array and all the objects inside it, you would do something like this -


Only after you instantiate the things inside the array can you call their methods or get their attributes. This would be why you were having a problem with the line -

in your code. The array had been instantiated, but the TextFields inside it had not.

-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
bobby, morkos
Ranch Hand

Joined: Jan 04, 2002
Posts: 82
Thanks a lot for your help. And if I'm correct to get the value of the textfield, I do something like this:
e.g. if there's 4 textfields.
for (int j=0; j<4; j++){
t[j].getText();
}

Originally posted by Nathan Pruett:
You are confusing an array of objects with the objects inside it...

This declares an array of Textfields:


This instantiates the array:


And this instantites an object in the array:


So, to create an array and all the objects inside it, you would do something like this -


Only after you instantiate the things inside the array can you call their methods or get their attributes. This would be why you were having a problem with the line -

in your code. The array had been instantiated, but the TextFields inside it had not.

-Nate
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Could you make dynmically textfields, labels, and buttons
 
Similar Threads
Conventions
There's a problem here
how to make a page type recordset
swing problem
GridBagLayout Compile problems