Search code examples
javanetbeansjform

Add new fields with press of a button


I´m creating a program in NetBeans where the user can create their own CSV-file. I've made a GUI. When I press the button, I want a new JLabel and a new JTextField to appear underneath the existing ones, for as many times as the button is pressed. How do I do that?


Solution

  • I've done something similiar and I'm using a JPanel with GroupLayout to solve this problem and that's my code:

    EDIT - Your question aroused my interest and I've changed my code to your needs (of course only the basic, you will have to improve it)


    Global variables

    private GroupLayout m_layout;
    private SequentialGroup m_verticalSg;
    private ArrayList<Component> m_labelList;
    private ArrayList<Component> m_textFieldList;
    private ParallelGroup m_horizontalPgLabels;
    private ParallelGroup m_horizontalPgTextfields;
    

    Method createLayout()

    Creates the layout for your panel which should contain the label & textfield components

    private void createLayout()
    {
       m_layout = new GroupLayout(YOUR_PANEL);
       YOUR_PANEL.setLayout(m_layout);
    
       //This SequentialGroup is used for the VerticalGroup
       m_verticalSg = m_layout.createSequentialGroup();
       m_verticalSg.addContainerGap();
    
       //Two ParallelGroups are used. One for all labels and the other one for all textfields
       m_horizontalPgLabels = m_layout.createParallelGroup(GroupLayout.Alignment.LEADING);
       m_horizontalPgTextfields = m_layout.createParallelGroup(GroupLayout.Alignment.LEADING);
    
       //These component lists are used for linkSize() -> Equalize components width
       m_labelList = new ArrayList<>();
       m_textFieldList = new ArrayList<>();
    
       m_layout.setHorizontalGroup(m_layout.createParallelGroup()
                        .addGroup(m_layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(m_horizontalPgLabels)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) //Create gap between horizontal groups
                        .addGroup(m_horizontalPgTextfields)
                        .addContainerGap()));
    
       m_layout.setVerticalGroup(m_layout.createParallelGroup().addGroup(m_verticalSg.addContainerGap()));
    }
    

    Method addNewRow()

    Call this method from your button click event

    private void addNewRow()
    {
       if(m_layout == null)
          createLayout();
    
       Dimension dimLabel = new Dimension(100, 15);
       Dimension dimTextfield = new Dimension(200, 20);
    
       //Create a new label
       JLabel lbl = new JLabel();
       lbl.setText("Your text");
       lbl.setIcon(null/*Your icon*/);
       lbl.setSize(dimLabel);
       lbl.setPreferredSize(dimLabel);
    
       //Create a new textfield
       JTextField txtField = new JTextField();
       txtField.setSize(dimTextfield);
       txtField.setPreferredSize(dimTextfield);
    
       //Add components to arrays and increase index
       m_labelList.add(lbl);
       m_textFieldList.add(txtField);
    
       //Create new ParallelGroup for the vertical SequentialGroup
       ParallelGroup newVerticalParallelGroup = m_layout.createParallelGroup(GroupLayout.Alignment.LEADING);
       newVerticalParallelGroup.addComponent(lbl);
       newVerticalParallelGroup.addComponent(txtField);
       m_verticalSg.addGroup(newVerticalParallelGroup);
            m_verticalSg.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED);
    
       //Add the new label to the horizontal label group
       m_horizontalPgLabels.addComponent(lbl, GroupLayout.Alignment.CENTER);
       //Add the new textfield to the horizontal textfield group
       m_horizontalPgTextfields.addComponent(txtField);
    
       m_layout.linkSize(SwingConstants.HORIZONTAL, m_labelList.toArray(new Component[m_labelList.size()]));
       m_layout.linkSize(SwingConstants.HORIZONTAL, m_textFieldList.toArray(new Component[m_textFieldList.size()]));
    }
    

    The last step is to add an ActionListener to your button to call the method addNewRow().

    jButton1.addActionListener(new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
           addNewRow();
        }
    });
    

    Feel free to ask me if something is unclear.