Friday, October 22, 2010

Code Templates in Netbeans

The code template feature of netbeans allows us to quickly write the frequently used code.

It is like the abbreviation that gets expanded to its long form on using that abbreviation. So in this article I will tell you some of the code templates that are already defined in the Netbeans, which are frequently used and the one which are useful and I will also tell you how to create our own code templates.

Before knowing the default code templates in Netbeans, we should first know how to use the code templates.
So suppose we want to use the code template for Boolean.  The abbreviation used is bl. So to write Boolean we just need to write bl and then press TAB. So as soon as TAB is pressed, the Netbeans will write the complete Boolean word for you.  

Netbeans is an intelligent editor which on expanding automatically identifies the variable on which the code is to be written. For example for the” for each” loop, the abbreviation used is fore. So whenever fore and TAB is pressed, it will automatically write the for each loop for you and it will automatically find the the variables for which you are writing that loop. So it is an intelligent  editor.

Here are some of the code templates :
  • en = Enumeration
  • ex =Exception
  • ob = object
  • pf = public final
  • pfm = this will write the method structure as :
            Public final void method()
  • pm = this expand the abbreviation as
                Public void method
  • ps=public static
  • psf public static final
  • st = String
  • al = to create a new array list.
  • bl = Boolean
  • br =break
  • dowhile = it will expand this abbreviation for do while loop
  • for = for loop
  • fore = for each loop
  • trycatch = it will expand it to try catch block
  • psvm= this will write main method.
  • sout = this will expand to “System.out.println(“”);
  • soutv=this is used when we want to print the values of variables. So this will be expanded to
                So this will automatically take the variable name and will write the code as above.

So these were the templates already present in Netbeans. Now suppose we want to create our own templates. So how to do? How to create our own templates.
I will show you how to create it.

Go to Tools->options.
No go to editor tab and then select code template.
The window will look like :

         (click on image to enlarge)

This will show you the list of all the code templates present.
Now to add the new template, click on new and enter the abbreviation. And click on OK. Now in the expanded text area, write the code you want to be expand on writing that abbreviation.
 Below you can see a combo box named “Expand template on”. This combo box lets you decide when to expand the text. Here default tab is selected. This means that whenever you will press TAB, your abbreviated code will be expanded.
Click on Ok to save the newly created template.

Saturday, October 16, 2010

Selecting the browser for web application in Netbeans.

By default, whenever we run the web application in netbeans, it chooses the system selected default web browser for running the web application. But there may b case where we need to test a particular web application on some other browser other than the default one.

So following will help you to choose the particular browser for testing web application.

Go to Tools -> options. You will see the following dialogue box:

Now click on the “web browser “ combo box to see the list of the browsers installed and available to chose.

Select the desired browser and  click on OK.

You can also specify the arguments if u want to give along with the broser. Click on “edit” button besides web browser combo box. Here is the screenshot of it :

now just run you web application and you will see that you web application is opened in the browser you have selected. ::)