Issue
Whenever I create new JPanelForm
, NetBeans will create some auto generated code in initComponents()
method. How can I remove this auto generated code from my JPanelForm
?
Solution
Using a GUI designer like Netbeans or Eclipse, will force you to accept certain conventions. One of these conventions is the automatic generation of initComponents()
method by Netbeans.
Although Netbeans is highly configurable and allows the user to modify lots of things, the initComponents()
method is always used by the GUI builder.
When you create a JPanel
Form in Netbeans this is the initComponents()
you get by default:
private void initComponents() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}
If you take a close look at it you will see it has only layout instructions. You may want to change the layout. Let's say you want to have a BorderLayout
for your JPanel
. Go to the Navigator, change the layout by choosing the right property and this is your new initComponents()
now:
private void initComponents() {
setLayout(new java.awt.BorderLayout());
}
If someone is in a Swing learning phase, it is wiser to avoid using a GUI Builder. Designing the components by hand will give a better understanding of how things work. The GUI builder will always be there to automate procedures once the principles of Swing have become familiar.
Answered By - Costis Aivalis
Answer Checked By - Willingham (JavaFixing Volunteer)