Customize Your NetBeans Java Template to Fit Your Need

NetBeans comes with generic class templates. Often times though, you may find yourself retyping or cutting-and-pasting the same generic text every time you try to create a new class (such as your company's copyright text, disclaimer, etc).

Wouldn't it be nice to have NetBeans create a java class that already has the information there? Yes, this can be done quite easily. Customized template to the rescue!

Let say you want to create a customize java class template with your company's information. Here are the steps to set it up (and these steps also apply to customize Java interfaces, applets, etc.) so here we go

  1. Choose Tools > Options. In the Options window, navigate to Source Creation and Management > Templates > Java Classes > Java Class
    image
  2. Right-click on Java Class and choose Copy
    image
  3. Go back up to Source Creation and Management > Templates > Java Classes
  4. Right-click Java Classes and choose Paste > Copy
    image
  5. You should now see Class_1.java as one of the templates under Java Classes.
    image
  6. Right-click on Class_1.java and choose Rename, the following window pops up. Modify the name to your company's name (we will use XYZCompany here) and click OK. (Do not end the template name with the word Template. NetBeans will not display your new template if you name it that way).
    image

    Click Next on the following pop-up window

    image

    and voilĂ ! your new java class template appears

    image

  7. You should now see Class_1.java renamed to XYZCompany.java. We are only half-way done here.
  8. Right-click XYZCompany.java and choose Open. The template source opens in the Source Editor. Click Close on your Options window to close it and go to your Source Editor.
  9. This is where the magic begins ... From the Source Editor, modify the source code however you want it. See example below (DO NOT change the class declaration. You can replace all other class name with __NAME__ macro which will be substituted by the actual class name when you create a file from the template) :
    /*
     * __NAME__.java
     *
     * Created on __DATE__, __TIME__
     *
     * Copyright(c) {YEAR!!!} XYZ Company, Inc.  All Rights Reserved.
     * This software is the proprietary information of XYZ Company.
     *
     */
    
    package Templates.Classes;
    
    import org.xyz.whatever         //  Place any imports that are common
                                    //  to most of your classes
    import org.xyz.util.Logger;     //  Logging library, for example.
    
    
    
    /**
     * {Insert class description here}
     *
     * @version $Revision: 1.1.1.1 $
     * @since Build {insert version here} (MM YYYY)
     * @author John Doe
     * @author $Author: nbweb $, (this version)
     */
    public class XYZCompany
    {
         private static final String PVCS_ID = "@(#)$Header: /cvs/testwww/www/competition/win-with-netbeans/customize-java-template.html,v 1.1.1.1 2009/10/29 15:04:23 nbweb Exp $";
        private static final Logger log = Logger.getLogger (__NAME__.class);
        private static final String CLASS_NAME = "__NAME__";
        
        
        
        /**
         * Creates a new instance of __NAME__
         */
        public XYZCompany ()
        {
        }
    
        
        
        /**
         *  Add other methods here ...
         *
         *  @param value1
         *  @param value2
         *
         *  @return
         *
         *  @throw
         */
    //    public void helloWorld()
      
    //    throws whateverException
    //    {
    //    }
        
    }    //  end __NAME__
    
  10. When you are done, click File > Save.
  11. Now, whenever you need to create a java class, you just need to choose to create a new XYZCompany java class and you will have all the customized information right there!

Set default user name and other macros

  1. Choose Tools > Options. In the Options window, expand Editing > Java Sources
    image
  2. Click on the ellipsis (...) button of the Strings Table value from the Properties tab on the right.
  3. Set USER=Your Name
  4. Add other macros in the form {Macro Name}={Macro Value} and click OK. (When you define the macro, do not place the underscore characters at the beginning and end of the macro name).
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo