• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Could you make dynmically textfields, labels, and buttons

 
bobby, morkos
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
bobby, morkos
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic