Archive for January, 2009


Netbeans Code Folding

A Very Happy New Year 2009 to everyone. I would like take this pleasure to wish all of you Good and to present you a nice gift by giving you tips on Netbeans Java editor. I have been developing in .Net for awhile and found that the studio have very nice feature which is enabling coder to group sections of code as a group with code folding/collapsing feature. This functionality already exist for netbeans by default for methods, and imports, but never explicitly done if a programmer wanted to group and fold a group of functions. However, it can be done. In this article I will show you the syntax to use and how we can make it into netbeans’s shortcut.

The syntax to use to group a collection of code is

//<editor-fold defaultstate=”collapsed” desc=”">

…..your code is here
//</editor-fold>

Defaultstate : This refers to the view state of to folding code when the editor is open. If collapsed is the value, the code will be showed, otherwise it will be folded.

Desc: This will the description visible when the code is folded.

Some sample of the folded and collapsed code is as follow :

Folded Code

Collapsed code

Now you know Netbeans is never behind Visual Studio at all and it is actually better.  :) Some of you would say:”Hei that is awful lot of things to type!! Can it be easier?”.  Answer is : “Certainly it can!”. Netbeans provide coder with a convinient way of putting shohrtcut key, for instance, instead of typing System.out.println(”"), just type “sout” + TAB, and Netbeans will complete the code for you effortlessly.

Now from your Netbeans, select the Tools menu and then the Options submenu.

Options

A dialog is presented and click on the Editor menu on the top. Then you will be able to locate the Code Complete Tab (should be the third tab). Click the New button and you will be prompted for the abbreviation for your new shortcut code template, in this case I put “fold” (You can put anything you like as long as it is not already exist in the table list).

The Option Editor settings

Abbreviation for shortcut

In the Expanded_Text Tab, enter the code as shown in the following picture:

The Code Complete code

Now, you are all ready for testing, try open any new/existing class and start inserting your “fold” shortcut, and you will notice that the cursor is nicely place in the description session for you to enter the grouping name/description. Wish you a Happy Folding 2009 :D