How to change non-editable/generated code in netbeans

Another option delete the special comments. You can't see in NetBeans but if you open in other text editor (for example notepad) you will see that comments on the begining and ending of non editable section. The comments look like this:

//GEN-BEGIN:initComponents
//GEN-END:initComponents

It depends on WHY netbeans is preventing you from editing that source file.

  1. It is part of some library you just import into your project. This means your project is really using the compiled class/jar files. There is no point in editing this file. Find the author of the library and file a change request. This also applies for the java API itself. Of course the chances that they will actually accept the change request might be slim.

  2. It is write protected on the disk for no special reason. => make it writable

  3. It is controlled by some version control system which prevents editing before checking a file out of the system. Well check it out.

  4. It is some file generated by netbeans (which might actually be the case, judging from the source code you posted). In this case you probably can edit it in another editor (notepad/vi), but your changes will probably be overwritten on the next code generation, or break the file for whatever tools you use. => Find the tool and how to apply the changes appropriately with that tool.


If you want to add custom component to a file that uses the Netbeans GUI editor (called: Matisse), there are several ways:

First:

You could add that component to the "Palette" manager. I, personally, have had limited success with this, especially when adding components from my own, custom libraries. However, you can add components and just select "from a project", then select your project and you should see your component listed (if you don't, run a clean & build).

Notice in this project I have two files, CustomTextField and NewJFrame.

enter image description here

The code for CustomTextField is just something that extends a JTextField:

package test4;

import javax.swing.JTextField;

public class CustomTextField extends JTextField {

}

Then, in the Palette window, you right click and click "Palette Manager..."

enter image description here

Then in the Palette Manager, you click "Add from Project..." (if you are adding from a library, you click "Add from Library...", etc.

enter image description here

Then I select CustomTextField

enter image description here

And boom there it is in the Palette Manager, and I can drag & drop it into my GUI.

enter image description here

Note: this way is not very good if you need this component across multiple projects. The palette manager is for Netbeans as a whole, so if you try to use a component that exists in another project, you'll have trouble.

Note #2: I had problems with Java Web Start and my own custom library. There is a bug that was released with 7u25 that causes a NPE from the JNLPClassLoader whenever my custom libraries are loaded, so I had to import the components from a Project instead of a Library.

Second:

There is a more hackish way of doing things, but sometimes it's necessary: In the GUI-editor, you can right-click the component and click "Customize code."enter image description here You can then change the variable definition (not the declaration). If you need to change the variable declaration, then it gets even MORE hackish, and I would recommend you just hand-write your code at that point. But, to do it through this window, you can "comment off" the portion in "variable declaration code" by adding /* and */ around private javax.swing.JTextField jTextField1; and add your own variable declaration.

Third:

Your other option is to hand write. :) If you need simple Swing components or components that can be added to the palette, I recommend the GUI Editor. If you need more complex and customized Swing components, then you'll want to start hand writing this code. Most people will recommend you do so anyways. I, however, proudly love Netbeans` GUI Editor.