Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change non-editable/generated code in netbeans

I want to change the non-editable code in Netbeans ,
I want to replace the
javax.swing.JTextFeild with ObservingTextField
for which I have a class imported into my project in order to Implement a date picker
But Netbeans Does not allow me to edit the code . Please help

// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
`private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;`
// End of variables declaration   
like image 248
CleanX Avatar asked Jun 16 '13 08:06

CleanX


People also ask

How remove generated code in NetBeans?

Go in the Design mode to the top window menu -> Windows -> Navigator . You will see Group "Navigator" popped up on the left (usually). Now you can delete unwanted variables (or edit them) with the right click menu.

How do I change Initcomponents in NetBeans?

If you right-click on the component in the Design View , and then click on " Customize Code " selection, you can modify the code in the InitComponent code.

How do I change the preview design in NetBeans?

Try this: Go to inpector view. Right click on your jFrame. Then in preview design select a L&F.

How do I change all instances of words in NetBeans?

CTRL+SHIFT+H will open the replace dialogue where you can find+replace in any or all of your open projects.


4 Answers

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
like image 196
Lanistus Avatar answered Oct 19 '22 17:10

Lanistus


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.

like image 36
ryvantage Avatar answered Oct 19 '22 19:10

ryvantage


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.

like image 38
Jens Schauder Avatar answered Oct 19 '22 19:10

Jens Schauder


Net beans let's you use custom code there from the form view page. I'm not on a computer with net beans right now but if you right click on the text area and its near the bottom

like image 40
exussum Avatar answered Oct 19 '22 17:10

exussum