1

I really appreciate how NetBeans automatically enters closing brackets to the code I'm working on. Would it happen to be possible for it to further streamline the development of code that looks like this?:

public class Test
{
    public static void main(String[] Args)
    {
        //Some code here

        while(true)
        {
            //Some more code here

        }//end while

    }//end main
}//end class

1 Answer 1

0

Unfortunately there doesn't seem to be a way to customize what is generated when NetBeans adds the closing bracket: there is just a checkbox for whether to do this or not in Tools->Options->Editor->Code Completion.

Here is the next best solution:

NetBeans features "code templates", which are snippets of predefined code that are automatically generated when you type some predefined abbreviation and then hit tab. For example, if you type if and hit tab, your editor should generate the following code:

if (true) {

}

You can modify these code templates to your needs or create new ones of your own. Go to Tools->Options->Editor->Code Templates. You will see an extensive list of templates, each matching an abbreviation, and a small edit box to change them. If you scroll down to "if" you'll see this definition:

if (${EXP instanceof="boolean" default="true"}) {
   ${selection}${cursor}
}

All you need to do is modify it to generate what you desire:

if (${EXP instanceof="boolean" default="true"}) {
   ${selection}${cursor}
}//end if

You can do something similar for other statements such as while. You'll notice there often are multiple templates per statement based on different patterns, so this could get tedious depending on how thorough you want to be.

This solution also works well for the main method, since there is a template defined for it - typing psvm and tabbing generates it. For other arbitrary methods, there is a template with the abbreviation m.

Achieving this effect for a top-level class will be different. Go to Tools->Templates. Here you'll see another set of templates which you can explore and modify. The difference is that these are for files rather than code snippets. Expand Java and open the Java Class template file. You'll see the following:

<#assign licenseFirst = "/*">
<#assign licensePrefix = " * ">
<#assign licenseLast = " */">
<#include "../Licenses/license-${project.license}.txt">

<#if package?? && package != "">
package ${package};

</#if>
/**
 *
 * @author ${user}
 */
public class ${name} {

}

Once again, you can modify this template to your needs:

public class ${name} {

}//end class

Or even:

public class ${name} {

}//end {$name}

which will put //end Foo for a class named Foo

1
  • Figured that would be my best shot. Thank you very much!
    – Orion751
    Oct 2, 2011 at 2:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.