I have simple form, and two Arrays, JLabels and JTextFeilds:
ArrayList<JLabel> jlabels = new ArrayList<JLabel>();
ArrayList<JTextField> textFields = new ArrayList<JTextField>();
I wand to add it to Frame and pack this elements correctly (please see image).
Please look at a piece of my code:
public class ProductForm extends JFrame {
private JPanel contentPane;
public ProductForm() {
initComponents();
actions();
}
public void actions() {
setTitle("Product selection");
setSize(600, 350);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
private void initComponents() {
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
ArrayList<JLabel> jlabels = new ArrayList<JLabel>();
ArrayList<JTextField> textFields = new ArrayList<JTextField>();
//for (int a = 0; a < product.list.size(); a++) {
for (int a = 0; a < 7; a++) { // let say 7 for example
jlabels.add(new javax.swing.JLabel());
textFields.add(new javax.swing.JTextField());
for (JLabel j:jlabels) {
contentPane.add(j);
}
for (JTextField f:textFields) {
contentPane.add(f);
}
}
pack();
}
As a result, I have this content: please see image below
Please help me how to pack this elements correctly. Should I initialize it in some way?
Thank you for any help.
Try this
Update