Chairperson and Program Coordinator for the Computer Science Technology Program at Dawson College Instructor and Program Consultant for the School of Extended Learning Computer Institute at Concordia University I have been passionate about programming since buying an Apple][+ in 1980. I paid the extra $450 to bring the RAM up to 48K! Ken has posted 16 posts at DZone. You can read more from them at their website. View Full User Profile

NetBeans in the Classroom: The Beginners Template (Part 2)

05.19.2014
| 1655 views |
  • submit to reddit
Ken Fogel is the Program Coordinator and Chairperson of the Computer Science Technology program at Dawson College in Montreal, Canada. He is also a Program Consultant to and part-time instructor in the Computer Institute of Concordia University's School of Extended Learning. He blogs at omniprogrammer.com and tweets @omniprof.

In my previous article I showed how I like a new project to present its generated class that contains the main method. NetBeans can be configured to do this for you by editing the code template for new Java Application projects.

Begin by going to the Tools menu and select Templates. A dialog will appear with a tree of choices. Locate Java and click on the plus symbol to open the tree to reveal the list of code templates available.

We are interested in Java Main Class. Select it and the buttons Duplicate and Open in Editor become active. First click on Duplicate to preserve the original template. The new template will be named Java Main Class 1

Select Java Main Class again and click on Open in Editor. This template will appear in the NetBeans editor.

The first change is to delete lines 1 through 5. These are the lines that produce the following in the class:

* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

In a commercial or open source environment all files must be branded. For students starting out these lines just clutter the files. That is why I recommend deleting them.

Now it’s time to change the template to match my recommendation. Here is what it should look like:

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

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

    /**
     *  Default Constructor
     */
    public ${name}() {
        super();
    }

    /**
     * Call upon the application logic
     */
    public void perform() {
        // TODO code application logic here
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        ${name} obj = new ${name}();
        obj.perform();
        System.exit(0);
    }

}

Save your changes and test that it has worked by creating a new Java Application.

In the template there are three expressions. The first is ${name} and that comes from the Project Name field. The second is ${package} and that comes from the filed next to the Create Main Class checkbox and includes anything up until the final period. The third is ${user} and that is the name you used to log into your system. I will write about what should go here is another article.

If all has gone well you should have a file that contains the following code:

package changingtemplate;

/**
 *
 * @author Ken
 */
public class ChangingTemplate {

    /**
     *  Default Constructor
     */
    public ChangingTemplate() {
        super();
    }

    /**
     * Call upon the application logic
     */
    public void perform() {
        // TODO code application logic here
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        ChangingTemplate obj = new ChangingTemplate();
        obj.perform();
        System.exit(0);
    }

}

This is what I want my students to see when they start a project. You can use this same technique to alter any of NetBeans’ various other file templates.

Published at DZone with permission of its author, Ken Fogel.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)