
how do i use loops(for,while) to add components in java

Hi Friend,
The below code might help you.
import java.awt.*;
import javax.swing.*;
class TextFieldArray extends JFrame{
JLabel l[]=new JLabel [5];
JTextField t[]=new JTextField [5];
public TextFieldArray(){
JPanel panel=new JPanel(new GridLayout(5,2));
for(int i=0;i<5;i++){
int j=i+1;
l[i]=new JLabel("Label"+j);
t[i]=new JTextField(5);
panel.add(l[i]);
panel.add(t[i]);
}
add(panel);
setVisible(true);
pack();
}
public static void main(String[] args){
new TextFieldArray();
}
}
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.