views:

1735

answers:

4

In NetBeans the GUI Builder generates code that is off limits to you from the editor.

I know you can switch to Design Mode and specify custom code and then enter edit in indirectly that way, but it's a pain to not be able to edit text that's 2 lines from your cursor.

Can I allow editing within those regions... I promise I'll be careful :)

Thanks.

Note: I don't want to lose code generation

+1  A: 

If you open the .java files in Notepad or WordPad, you'll find pairs of comments that say

// GEN-BEGIN: <name of section>
...
// GEN-END: <name of section>

Delete those comments, and two things will happen.

  1. You'll be able to edit anywhere in the file in NetBeans.
  2. NetBeans will not be able to regenerate the file from the GUI editor.

So it can be done, but don't do it unless you really mean it.


EDIT: "Note: I don't want to lose code generation." Ok, let's see here.

  1. You want to change what is generated.
  2. You don't want to lose code generation.
  3. You don't want to use the facilities NetBeans provides for adding custom code.

It sounds like you're wishing yourself trouble. :D

Michael Myers
I know that. :) Was looking for something that didn't require me to blow away the existing code generation stuff.
Allain Lalonde
Perhaps. Even an "Open as Text" feature would do.
Allain Lalonde
But even if you did open it as text, your changes would be destroyed the next time you changed the form.
Michael Myers
A: 

Can you subclass the class and redefine the method where this code is? Of course then you'll have to maintain the method over time as you make changes to the GUI.

You can maybe also look into AspectJ. It will modify the byte-code at build time and make the changes you define. Not sure if this gives you enough control over what code to change.

sjbotha
Don't want to change the code, just the IDE's behaviour. Because I can make it work as is, but it's annoying.
Allain Lalonde
You can write plugins for Netbeans. I've seen some tutorials around for doing that. Not sure if it's possible to change this behavior or how hard it would be.
sjbotha
+1  A: 

If you open it in an external editor, there are two possibilities:

  • there is no guarantee that the changes you make will work (and not break anything) in the Netbeans UI Editor
  • The next time you edit the file in Netbeans it will be regenerated.

Either way, probably not what you want. The best thing to do would probably be to make the whole design up in the gui editor and when you are happy with it, delete the comments and continue on.

[sarcasm] With the drag and drop gui editor, what could you possibly want to change by hand anyway? [/sarcasm] ;)

SeanJA
A: 

If you use APT you can rewrite source code at compile time. This way you can overwrite the code that's there at the moment. You could keep the new code in the same Java file along with comments to explain what is going on. This tutorial makes it pretty easy to get started. Copy that code, get it working, then tweak it to your liking. To find out where in the code the annotated method is call getPosition() on the Declaration.

sjbotha