Quickstart NetBeans Editor Shortcuts and Code Templates

NetBeans is an optimal Java development environment. Just recently, while visiting the JAX Conference in Mainz/Germany, I attended a live coding session by Adam Bien. I was telling later a fellow developer and Eclipse user how convenient NetBeans is and how Adam, even though he broke his arm in a bicycle accident, used NetBeans during his live coding session. Later I met my colleague again during lunch time: he just had installed NetBeans on his notebook and was already fond of the NetBeans usage experience.

And yes, numerous keyboard shortcuts and code templates make editing and writing java classes with NetBeans a breeze. Here are my favourites to give you a quick start within the NetBeans Code Editor (in brackets the Eclipse pendants) :

Keyboard Shortcuts

  • Alt+ENTER (Ctrl+1)
    Fixes Code – whenever there is a yellow light bulb on the left hand side of the editor, try Alt+ENTER: a list of suggestions how NetBeans can fix the code for you pops up. It also may add missing JavaDoc and assigns a variable to the returned value of a method call.
  • Alt+Insert
    The code generation menu opens up and let’s you insert getters and setter, equals and hashCode, toString and many more.
  • Shift+ENTER (same)
    Insert a new line and move the cursor to the new lines beginning.  Shift+ENTER may be pressed at any position on the current line without splitting it.
  • Ctrl+e (Ctrl+d)
    Removes the current line.
  • Shift+Alt+period/comma (Alt + Shift + Arrow Left/Right)
    Selects next/previous element (such as name, expression, line, method body, method etc).
  • Ctrl-r (Alt+Shift+r)
  • Ctrl-o (Ctrl+Shift+t)
    Goto Type
  • Shift+Alt+o (Ctrl+Shift+r)
    Goto File
  • Shift+Ctrl+Arrow Up/Down (Ctrl+Alt+Arrow Up/Down)
    Copy line up/down
  • Shift+Alt+Arrow Up/Down (Alt+Arrow Up/Down)
    Move line up/down
  • Ctrl+Arrow Up/Down (same)
    Scroll without loosing cursor position
  • Shift+Ctrl+c (same)
    Comment/Uncomment line
  • Shift+Alt+f (Shift+Ctrl+f)
    Format file
  • Shift+Ctrl+i (Shift+Ctrl+o)
    Organize imports
  • Shift+Ctrl+v
    Insert clipboard content formatted
  • Ctrl+F12 (Ctrl+o)
    Select Navigator Window. Inside start typing method names and the matching method will be selected. Press ENTER to navigate to it in your source file. Very handy!
  • F7/F8 (F5/F6)
    Debugger: Step into/Step over
  • Ctrl+Shift+ENTER (Ctrl+m)
    Show only Editor (hit key combination a second time to return to normal)
  • Ctrl+s (same)
    Save File
  • Ctrl+g (Ctrl+l)
    Goto Line

Code Templates

Code Templates may be entered at any line in the current file. After hiting the TAB-Key, the template is expanded.

  • pu
    Expands to “public”
  • pr
    Expands to “private”
  • re
    Expands to “return”
  • fore
    Creates a for-each loop wih smart detection of collection classes to use.
  • Pm
    Creates a skeleton public method declaration.
  • pm
    Creates a skeleton private method declaration.
  • sout
    Expands to “System.out.println(“|”)”

If you never tried the code templates before, you should at least try the method declaration templates (pm and Pm). They are clever implemented and really convenient to use.


Integrate JBoss AS Maven Plugin with NetBeans Actions

Deploying JEE Artefacts such as EAR Files to a JBoss EAP Server instance is unfortunately not working with NetBeans out-of-the-box. The NetBeans Application Server Plugin as well as the additionally available Wildfly Plugin do not help. If you are a corporate developer whose company licensed a fairly new JBoss EAP 6.2.x (at least at the time of writing this entry) – you’re out of luck.

Fortunately NetBeans excellent maven integration comes to the rescue in form of the JBoss Application Server 7 Maven Plugin. There is also Wildfly Maven Plugin available. Just add the following lines to your pom.xml’s “plugins” section (in my case a maven EAR module):


Hostname, port, username and password have to be changed according to your needs.
Now you have quite a few additional maven goals at hand – deploying, undeploying and redeploying as well as starting and stopping the JBoss EAP Server instance. I usually have an already running JBoss instance on my notebook, hence the deployment related goals matter to me:

  • jboss-as:undeploy — undeploys your already deployed artefact from the running JBoss instance
  • jboss-as:deploy — deploys the artefact that is generated by your maven project to the running JBoss instance
  • jboss-as:redeploy — redeploys your artefact

You could bind the redeploy goal to the packaging phase, so that every time the artefact is built, it is also redeployed to your local JBoss EAP Server instance. I prefer explicit execution of the redeployment goal. NetBeans offers two ways doing that:

Deploy through the NetBeans Navigator Window

Select the maven artefact project – in my case this is the pla-ear-2.0.0. The Navigator Window displays all maven goals that are not bound to maven life cycles. Double click the jboss-as redeploy goal and 8 seconds later, the artefact is deployed and ready to be debugged. At least when you have your debugger attached to the running JBoss EAP Server instance.


Deploy with custom NetBeans Actions

I have three custom actions, that let me deploy, undeploy and redeploy the built artefact to my locally running JBoss EAP Server instance. The actions can be reached through a right click on the maven JEE artefact project node. It opens up the projects context menu. Here select the Custom entry, that again opens a sub-menu with all your custom NetBeans actions refering to maven jboss-as plugin goals.


Set-up the goals as actions in the projects properties screen. Select the Actions category.


With the Add Custom… button I created three additional actions: Deploy, Undeploy and Redeploy. For each action¬† fill in the relevant maven goal: jboss-as:deploy, jboss-as:undeploy and jboss-as:redeploy. These show up later under the projects context menus Custom entry.

Make sure to uncheck the Compile On Save checkbox! Otherwise NetBeans own server deployment mechanism interferes with the maven plugin:


Happy deploying!

NetBeans join line macro

I was searching for a way to join lines in NetBeans in the same way you can join lines in vi (shift+j) or Eclipse (ctrl+alt+j). As far as I know, there is no predefined way to achieve this in my favourite IDE.

The powerful macro dialog lets you design your own editor macros and assign a keyboard shortcut to it. Hey – sure enough – here it is, the “join line macro”:














NetBeans Smart Semicolon — for German Keyboards

You know it – at least when using the Eclipse Code Editor: type a semicolon while editing a string literal and the semicolon will be printed at the end of the line and your cursor is placed after the semicolon. Unfortunately this happens even then when you intend to add a semicolon to the string literal. A similar function is also included in the NetBeans Java Editor. It is implemented in a more practical way I think: Ctrl+SEMICOLON adds a semiciolon to the end of the line and Ctrl+Shift+SEMICOLON adds a semicolon to the end of the line and additionally creates a new one (in other words: adds a line break after the semicolon).

If you live in Germany, that shortcut does not work: the semicolon is on the same key as the comma and only reachable if you hold the shift key pressed! But that makes an other key combination and the result is not what you want. We need to addapt the NetBeans keymap for the java editor. The keymap is found under “Tools/Options”.

Options Dialog

After clicking the “Keymap” icon on the Options dialog, the key bindings are displayed. The “Search” textbox allows to find the needed entries “Complete Line” and “Complete Line and Create new Line” quickly. In the picture above I simply entered “com” for “complete” and the search already hit the right entry.

In the shortcut column for “Complete Line” hold the Ctrl key and type comma. The key combination “Ctrl+COMMA” is entered in the column. For “Complete Line and Create new Line” hold down the Ctrl and Alt keys and type comma. The key combination “Ctrl+Alt+COMMA” is entered in the respective lines shortcut column. Both key combinations are already reserved for other shortcuts. A warning is displayed. Ignore the warning, click “Apply” and you’re done. Surely: if you want to keep the original key bindings, you have to come up with your own!

This fixes two very welcome editor shortcuts for the german keyboard layout. Try it out!

For even more cool shortcuts and hints have a look here (blog post in german).