Search code examples
javaswingjframejtextfieldform-layout

Pack correctly arrays of elements on JFrame


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). enter image description here

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 enter image description here

Please help me how to pack this elements correctly. Should I initialize it in some way?

Thank you for any help.


Solution

  • Try this

    private void initComponents() {
                contentPane = new JPanel();
                contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
                contentPane.setLayout(new GridLayout(7, 2)); // 7 rows and 2 cols
                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
                    JLabel jLabel = new javax.swing.JLabel("New Label");
                    jlabels.add(jLabel);
                    JTextField jTextField = new javax.swing.JTextField();
                    textFields.add(jTextField);
                    contentPane.add(jLabel);
                    contentPane.add(jTextField);
                }
    
                pack();
            }
    

    Update

    private void initComponents() {
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            contentPane.setLayout(new GridBagLayout()); // 7 rows and 2 cols
            setContentPane(contentPane);
    
            GridBagConstraints constraints = new GridBagConstraints();
            constraints.anchor=GridBagConstraints.NORTHWEST;
            constraints.weightx=0.5;
            constraints.weighty=0.5;
    
    
            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
                JLabel jLabel = new javax.swing.JLabel("New Label");
                constraints.gridx=0;
                constraints.gridy=a;
                jlabels.add(jLabel);
                JTextField jTextField = new javax.swing.JTextField(10);
                textFields.add(jTextField);
                contentPane.add(jLabel, constraints);
                constraints.gridx=1;
                contentPane.add(jTextField, constraints);
            }
    
            pack();
        }