a netbeans rcp blog

Archive for July, 2011

Work quicker in NetBeans

Posted by Nicklas Löf on July 12, 2011

Usually I’m writing about NetBeans RCP applications but this time it will be about NetBeans as an IDE. I’m going to share some tricks I use daily to work faster when writing code.


This is the ultimate shortcut that everyone should know about and use. It triggers different behaviors depending on where it’s executed. Normally it’s used to bring up the “light bulb-menu” that shows errors, warnings and hints that is shown on the left part of the editor. So there is no need to find your mouse and move it over there and click on it. Just hit alt+enter and the menu is shown.

Assign to a variable

If you, for example, has called a getter-method you can assign the returned object to a variable quickly by hitting alt+enter and NetBeans IDE will automatically lookup the type and create a variable for you.

Adding fields quickly.

If you have a constructor that takes a parameter you can hit alt+enter when the cursor is placed over the parameter and NetBeans will both add a this.variable = variable  to the constructor body and also create the field needed in the class (as a private final).


Another time saver and a great way to explore the APIs. I guess most people knows about this one but maybe not all the features.

The basic usage is to quickly lookup what Object you can create and what methods that are available on an object. So after you have type new just hit ctrl+space and it will show a list of all aviable classes that you can create an instance of and it will filter the list while you type. And to show the methods available on an object just hit ctrl+space after the dot and those will be shown with the javadoc. So this is something most people knows about and use ctrl+space for..  but there is more!

Auto generate variable names

If you for example adds a new variable in a constructor just enter the type and ctrl+space and NetBeans will show a few example of the variable name that you can use. This also work in the body of a method.

Override methods.

If you place the cursor outside a method body and hit ctrl+space it will show a list of available methods that can be overridden if you class is extending another class for example. And it’s clever enough and just shows the ones you haven’t override already.

Create getter and setters.

Just like the example above. Place the cursor outside a method and hit ctrl+space and start type get or set and it will show a list of getters and setters it can create for fields in your class that doesn’t have ones yet.


Hit ctrl+space and type the name of your class and it can autogenerate the constructor. And if you have fields in the class that can be populated from the constructor the different alternates of constructors is shown too.

Camel casing.

All the filter-while-your-type can be camel cased. So if you are looking for ArrayList you don’t have to type Arra.. to see it in the drop down menu. Just type AL and NetBeans will automatically try to match that to ArrayList. (or HMap for HashMap..  and so on)

Code Templates

NetBeans IDE has many predefined code templates that not only will generate a small block of code for you,it will also generate the code based on the current context and tries to autofill types and variables for you. There is lots of them available and to see a list of all just open up the Options window and look in Editor and the tab Code templates. Here you can also add your own ones.


This is my favourite. It saves lot of time while creating for each loops. Type fore and tab once. Now you can see the basic structure of a for each loop. NetBeans tries to lookup and Collections in the class you are editing and sometimes it gets it right and sometimes now. But instead of correcting the type, variable name and the collection by hand just hit tab twice and you will be placed in the collection field. Here you can now start typing the name of the collection (use ctrl+space to quickly see and filter ones that are available) and when you have selected the collection you want it will automatically change the type and variable name. And you aren’t limited to Collections in the current class. You can call for an static method somewhere else in your application that returns a Collection and NetBeans will automatically generate the loop your you.


Like above but quickly create for (int i=0….)-loops.


(Thanks to my collegaue Eric Aili that showed me this one yesterday). Just type lkp + tab and start to enter the Type you want to query the Global lookup for and NetBeans will auto generate the Lookup call, the Type and variable name.


Like the one above but for creating new objects of a class. Type newo + tab and enter the Type and the rest is autogenerated.


This one is shown if you hit alt+insert (ctrl+i on Mac) and shows a list of things that NetBeans can automatically create and insert for you in the current class.

Getter and Setters

Quickly populate the class with all the getters and setters. When it’s selected it will show a window with all fields in the class that currently doesn’t have a getter or setter. With the checkboxes you can selected which fields you want to generate for and after hitting Ok those will be created with the correct types and everything needed. If you just want to create one it might be quicker to use the ctrl+space shortcut shown earlier.


Same as above but you can choose which fields in the current class that can be populated from the Constructor method.

Add property

This one can save lot of time if you are creating a new property. It allows you to select the type and the name. The access and if NetBeans should create Getters and Setters for you. And this property mini wizard is especially useful if you are editing a class with propertychange support since it will take care of adding the code to fire the property change in the setter.

Override method

I usually use the ctrl+space way to do this but if I’m not sure excactly what I’m looking for this one is handy since it shows a browsable list of all avalable methods that can be overridden and I can select multiple ones. The drawback from the ctrl+space way is that you can’t see the JavaDoc of the method.

So this is some of the shortcuts and tricks I use daily to speed up writing code and once again..  alt+enter alt+enter alt+enter! Stop finding your mouse when you see the lightbulb and keep the hands on the keyboard.


Posted in Netbeans | 1 Comment »