1

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?

1 Answers1

1

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.

Yannick
  • 813
  • 8
  • 17
  • I get an error with YOUR_PANEL, what should I put there instead? – HaroldFinch Aug 22 '17 at 08:43
  • YOUR_PANEL is a place holder and should be replaced with the name of your panel. What's the problem? Maybe I can help you – Yannick Aug 22 '17 at 14:36
  • 1. The global variables need to be at the begin of your class, 2. Copy the methods `createLayout()` and `addNewRow()` into your class, 3. Add an ActionListener to your JButton in the constructor and call the method `addNewRow()` within this listener – Yannick Aug 23 '17 at 10:55
  • Just to make it clear: I've created a test Frame which contains a JButton and a JPanel – Yannick Aug 23 '17 at 11:19
  • I got the code to run without any errors, but nothing happens with my GUI. – HaroldFinch Aug 23 '17 at 12:18
  • I've uploaded my test class. It contains an own main() method, which allows you to run the single file and test it. Compare it with your code to find the problem. Download link (valid for **30** days): [TestFrame](https://ufile.io/6fflx) – Yannick Aug 23 '17 at 13:11
  • THANK YOU I got it to work :) I'm gonna use your code to shape my GUI to my preferences. Many thanks! – HaroldFinch Aug 25 '17 at 17:58
  • Very great, Congratulations, you're welcome! It would be nice if you accept my answer! – Yannick Aug 26 '17 at 21:15
  • Sorry, kinda new to this site. I'm trying to paste your code into my GUI but still get a ton of errors... – HaroldFinch Aug 27 '17 at 18:31
  • Try to implement your code in my already working test form. Otherwise you could upload your erroneous file, send me the link and I'll fix it for you – Yannick Aug 28 '17 at 10:15
  • Here's the code my NetBeans IDE generated as I created the GUI. The idea is that when you press the "Add field" button, there will a new label and a textfield appear underneath the previous one, with the same gap in between them. Hope you can help. Thank you ever so much! https://ufile.io/5ykhi – HaroldFinch Aug 28 '17 at 11:03
  • Please upload the `.java` **and** the `.form` file – Yannick Aug 28 '17 at 13:16
  • 1
    YES, you've no idea how happy you made me. Let me know if I can somehow return this favor! – HaroldFinch Aug 28 '17 at 15:32
  • I'm glad that it helped you! – Yannick Aug 29 '17 at 06:53
  • Can I ask one more thing? Whenever I'm trying to make your jForm appear with: CreateCSV C = new CreateCSV(); C.setVisible(true); nothing happens... This works fine when I create a form with swing. – HaroldFinch Sep 05 '17 at 14:39
  • Of course. I assume that you are trying to display a `JPanel` **standalone** and that's not possible. You have two options: 1. Change your _CreateCSV_ class from `extends JPanel` to `extends JFrame` or show your _CreateCSV_ panel within a JFrame object. Look here for more help: [JPanel doesn't show up](https://stackoverflow.com/questions/11920461/jpanel-doesnt-show-up) or [Display java JPanel in a JFrame](https://stackoverflow.com/questions/5364160/display-java-jpanel-in-a-jframe) – Yannick Sep 06 '17 at 07:41
  • I want your GUI to appear when I press a button in another GUI. When I change it to extends JFrame, I get a ton of errors... – HaroldFinch Sep 06 '17 at 07:49
  • Okay, you could leave it as it is and use my code out of the `public static void main()` method to show up the panel. Copy the code within the `run()` method braces and paste it to the other class where you want to show up `CreateCSV` form – Yannick Sep 06 '17 at 08:41
  • Then you've done it wrong. I think you have to learn the handling of `JPanel` and `JFrame` or `JDialog` in general, before you go on with your CreateCSV project. Try out some tutorials with easy examples. – Yannick Sep 06 '17 at 09:00