'How to remove auto generated code from Netbeans

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 1:[1]

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.

Solution 2:[2]

The Problem is that for a class XYZ with generated code there is a file XYZ.form (Same folder as the XYZ.java file). Netbeans unfortunately hides this file in it's Files / Projects Views, so you have to remove it by other means. After the .form file is removed the design tab for this class should disappear in Netbeans and you can delete the auto-generated code.

Solution 3:[3]

Maybe you can check this post out, and find the answer you are looking for. Remove Netbeans Auto Generated Code After Double-Clicking an Object in Design Tab

Solution 4:[4]

In the Design mode you select the particular object and click Events under Properties and remove the event from the event property.

Solution 5:[5]

I know this is a very old question, but I thought it may help you or someone else :)

In NetBeans, when you go on a component, there should be a "Properties" section (You can also access it by right-clicking the component), in that section, you should find "Properties", "Events" and "Code", go to "Events" and you should find the events related to that component there. Find the even that you'd like to remove, and click the small 3 dotted button, you'll see the handler, click on it and then click remove. That should fix it through NetBeans. Below, I'll explain another method that sometimes works, but not always (I think).

This is rather an unusual way, but I managed to work around the application and edited those parts. What you can do is open the actual code file using any other text editor, using the "Open with" function (On windows), and search for the part that you want to change, then change it through that text editor, save the changes, and boom! You've tricked the application :)

However, keep in mind that this might cause some errors and you'd have to fix them yourself using the same method, because the code is built upon itself automatically obviously. Remember that you're a problem solver, you should always try to think of a solution for your problems ;)

I hope this helps! Good luck! Have a great day :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jiaweizhang
Solution 2
Solution 3 Community
Solution 4 Juned Ahsan
Solution 5