You are trying to add to a panel using GroupLayout . Dynamically adding to this would be quite difficult (if possible), and I do not recommend doing this. I recommend adding the JPanel helper to the panel and adding any new components to this sub-panel.
EDIT:
To clear the solution used: the auxiliary JPanel was added using the form editor ( jPanel3 in the code below). The default form designer also uses GroupLayout for the added panel, which will lead to the same problem that should have been resolved in the first place, so the auxiliary panel layout manager has been changed to FlowLayout . The action code ends as simple as:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { JLabel jLabel2 = new JLabel(); jLabel2.setText("this is a label"); this.jPanel3.add(jLabel2); this.jPanel3.revalidate(); }
source share