TaT CreateJavaBeanPropUsingCodeTemplates

Easily Create JavaBean Properties Using Code Templates in NetBeans

Contributed By; Wade Chandler


NetBeans has a great feature called Code Templates. This is used to create shortcuts which may be typed in the editor then expanded into a template which the editor will ask the user to fill in the blanks. There are many useful ones which come configured upon installation within the NetBeans IDE, but users can also create their own custom code templates.

Writing Custom Code Templates


Custom code templates make it easy to create code templates to create JavaBean properties without doing anything other than typing shortcuts.

  • Below is a code template to create plain, un-bounded properties not incorporating property change events. It isn't much work to transform it into your choice of other useful property creating templates. The code template is;
    private ${TYPE} ${VAR} = ${VAL};

    public ${TYPE} get${NAME}(){
    return ${VAR};
    }

    public void set${NAME}(${TYPE} ${VAR}){
    this.${VAR} = ${VAR};
    }

    ${cursor}
  • In your NetBeans IDE installation name this code template 'prop'. Then, all you have to do in the Java editor is type
    prop
then press the TAB button/key. The template is expanded and asks you to fill in the details. Below are some screen samples of this in action.

File:ComDocEasilyCreateJavaBeanPropertiesUsingCodeTemplates/NB template1 TaT CreateJavaBeanPropUsingCodeTemplates.png File:ComDocEasilyCreateJavaBeanPropertiesUsingCodeTemplates/NB template2 TaT CreateJavaBeanPropUsingCodeTemplates.png

  • Simply press the TAB button to jump between the fields TYPE,VAR, and VAL, enter the values, and when you have them all filled in press the ENTER button and the cursor is placed at the ${cursor} position or offset in the editor.

Final Outcome


  • The final result is a read-write property with the source code all laid out nicely

File:ComDocEasilyCreateJavaBeanPropertiesUsingCodeTemplates/NB template3 TaT CreateJavaBeanPropUsingCodeTemplates.png

  • To expand the function of the template is easy enough. You can have one which adds the logic for property change events. Named it 'eprop' in your IDE.
    private ${TYPE} ${VAR} = ${VAL};

    public ${TYPE} get${NAME}(){
    return ${VAR};
    }

    public void set${NAME}(${TYPE} ${VAR}){
    ${TYPE} lold${VAR} = this.${VAR};
    this.${VAR} = ${VAR};
    pcs.firePropertyChange("${VAR}",lold${VAR},${VAR});
    }

    ${cursor}
It assumes you have an instance of java.beans.PropertyChangeSupport called pcs


  • Another one named 'ewprop', which stands for Wrap or Wrapper in this context, can be used to create a property incorporating property change events.
  • In this case, another field is needed to handle wrapping primitive types to pass the call to firePropertyChange, and it also assumes you have already defined a variable named pcs:
    private ${TYPE} ${VAR} = ${VAL};

    public ${TYPE} get${NAME}(){
    return ${VAR};
    }

    public void set${NAME}(${TYPE} ${VAR}){
    ${TYPE} lold${VAR} = this.${VAR};
    this.${VAR} = ${VAR};
    pcs.firePropertyChange("${VAR}",new ${WTYPE}(lold${VAR}),new ${WTYPE}(${VAR}));
    }

    ${cursor}
  • And, that covers much of what one wants to do with beans. You can expand upon all these ideas to come up with any number of properties which make your task of working with JavaBeans easier.
  • One may wonder why another wouldn't simply use the JavaBeans editing capabilities of the IDE for such things, but the truth is once some get used to using code templates they are easier to use as they do not require one to remove their hands from their home keys on their keyboard.
Give it a try, comments/suggestions are welcome- mail at dev AT usersguide DOT NetBeans DOT org
Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo