X
  • August 13, 2012

Ten Time-Savers in NetBeans

Tom McGinn
Learning Architect, Database User Assistance

The NetBeans IDE(Integrated
Development Environment) has come a very, very long way. Our Java
curriculum development group uses this tool everyday in the development of training materials and NetBeans is the default development environment
for students. As a result, we have compiled a set of ten time saving features that
we thought you ought to know about this powerful tool - features that
make the development of Java software easier. So counting down the top
ten (and these are no particular order), let's start by reducing the amount of typing needed for common methods.

Number 10: Use typing shortcuts!

Two of my most used shortcuts are psvm and sout. Huh?

NetBeans
has defined a number of code templates (abbreviations) to reduce the
amount of typing required for common methods, field declarations and
more. The two that I use the most are "psvm" to declare a public static
void main (String args[]) {
method, and "sout" to declare a
System.out.println(""); method. To use this functionality, simply put
the cursor into you Java class and type the characters, psvm, followed
by the Tab key:

That's a lot less typing! My second most used shortcut is sout, to create a standard console printed message:

Notice that NetBeans also places the cursor in exactly the right place -
you simply start typing right after the Tab key. A list of shortcuts
and a number of other helpful features are listed on the NetBeans
ReferenceCard, which is available through the Help menu or online at http://netbeans.org/project_downloads/usersguide/shortcuts-72.pdf.
You can view the complete list by opening the Tools->Options menu
within NetBeans and viewing the Editor->Code Templates and the
Keymap sections. There you can view, customize, and even define your own
shortcuts.

Number 9: Comment out a block of code

Sometimes
you need to comment out a block of code, either to test to see if
something is working (or broken) and sometimes because you changed your
thinking on some program logic. I find that deleting code is not a
good idea - you might find that what you thought you didn't need was
actually really important! So instead, select the code and comment it
out in a one action with Ctrl-Shift-C. And, by the way, it works in reverse too - uncomment that code later with the same key sequence. If you prefer using the mouse, you can also use the buttons  at the top of the editor window to comment and uncomment.

Number 8: Use global replace

Sometimes
you create a field or a method name that after careful thought, well,
it just isn't right.... Rather than hunting through a long class and
laboriously replacing each occurrence one at a time, NetBeans has a way
to replace every instance in your class all at once!

For
example, in this code fragment, empArray really doesn't say what this
field will contain - and, it isn't even an array. So what I really want
is to rename every instance of empArray to employeeData, all at once.
Simply click in the field, and all of the fields in the class will
highlight. Then press Ctrl-R (or right-click and then Refactor -> Rename) to start replacement.

Voila! Note: unfortunately, this doesn't work if your
code has unsresolved elements - so do this after your code compiles
correctly.

Number 7: Use auto format

Nobody
likes ugly code! In fact, 4 out 5 Java developers will refuse to work
on code that is not properly formatted. Ok, I made that up. But imagine
that you are trying to debug the code on the left (with apologies to the
Java tutorial). To make it easier to read, and determine the actual
flow, simply press Alt-Shift-F (or
right-click and choose Format) to reformat the code. the result is the
image on the right. This feature will also take care of leading and
trailing space - it does not remove blank lines between code lines,
which is nice if you want to separate methods and fields from each
other.

Number 6: Which file am I working on?

As
course developers, we often have files with the same name that belong
to different projects open in the editor at the same time. One file
belongs to practice 1, and the other practice 2. More than once I've
been editing a file only to discover it was in a different project than
the one I had open. There is a great feature in NetBeans to determine
which project a file belongs to. In the image below, I have two
Employee.java files open. If I click in the file and press " style="background-color: #ffff00;" class="bki-span">Ctrl-Shift-1
(or right-click on the tab that contains the file name and choose
"Select in Projects"), the project that contains that file will open in
the Project Tab and highlight the file.

Number 5: Close all open files

At
some point, you will have opened 10, 20 or more Java class files, XML
files, and other assorted files all at the same time. Sometimes, when
there are that many files open, trying to remember what you were
working on and what you need to do becomes overwhelming. The answer?
Reboot. No, not your machine, reboot your editor pane! Press Ctrl-Shift-F4
and all of the open file will close. If you had any open with unsaved
edits, you will get a prompt to save the files. Then you can start your
thinking process again with a clean slate.

Number 4: Fix imports automatically

When
you are writing code, stopping your train of thought to track down a
missing import statement, and resolve compilation errors such as "cannot
find symbol" could be a tedious chore if you had to do it yourself. One
of my favorite features in NetBeans is Ctrl-Shift-I
(Fix Imports). This simple keystroke combination will track down
missing import statements and fill them in for you, automatically. And
if there are two classes with the same name in different packages and
Fix imports can't figure out which class you want from the other import
statements, it will allow you to choose which one you mean to include.

But wait, there's more! Fix imports will also remove extraneous imports and order them, neat and tidy!

Number 3: Make the javadoc work for you - use autocomplete!

Anyone
who has memorized the entire javadoc, all the packages, classes
and methods, well, that person has too much time on their hands! When
we are coding, we understand what we are trying to do in the code, but
not necessarily all of the method names and parameter syntax. Instead,
make sure that you have the javadocs you need and use autocompletion to
help choose which method you want, which field, which enum, even which
tag in JSF and JSP files. Simply type a part of a class, a method name
or the . (dot) to evaluate which method the object can call and press Ctrl-Space.

BTW, because NetBeans is always trying to help out, you may have seen this popup and go away without
really knowing how to get it back, so know you know - Ctrl-Space.
Notice in this simple example, I typed part of the ubiquitous
System.out, and then after the dot pressed Ctrl-Space and selected the
append method. On top is the javadoc and below is the method signature I
could select simply by clicking on it.

Number 2: Auto-generate getters and setters, constructors and more!

The
JavaBeans pattern is one of the most used coding patterns. Simply put, a
JavaBeans has properties that are accessed or mutated through
methods. Most often the properties represent fields in your class. The property methods follow a pattern, with getXXXX and setXXXX, where
XXXX is the property (field) name. The simplest way to generate this code is to
create your class, add the fields you want, click in the line after the
fields and select Alt-Insert
to open the Generate dialog and choose Getter and Setter. Then
individually choose the fields, or select the class to generate a getter
and setter for every field!

 Poof! As if by magic I have a set of 5 getters and 5 setters inserted into my Employee class.

Using the same Generate feature above, you can also
generate constructors, loggers, override Object equals, hashcode and
toString, override methods of the parent class and more.By generating a
constructor and then just getter methods, you have a nice immutable
class.

Number 1: Compare files to see what's changed

Probably
the best kept secret of NetBeans is the Diff Tool. We use this tool a
great deal when creating training, as the labs we write tend to be on a single project that changes from one lab forward to the next. As a result, we often have copies of classes with differences based on the lab. However, for anyone creating
copies of files and making changes between copies, this tool is an
absolute time saver! Simply select one file, then click Tools -> Diff
and choose the second file. they don't have to be the same name or even
be open at the same time.

As shown in the image below, the diff tool
has two modes, one to graphically show the differences and a textual
differences tool as well. You can swap left and right, export the
differences to another file and even set the granularity of the
differences with Options. But if you're like me, just the standard
graphical view tells the whole story in glorious color!

Join the discussion

Comments ( 16 )
  • guest Monday, August 13, 2012

    What to say else. For sure my students use it every day and they become locked to netbeans. Personaly tne number 10 is new for me.

    Good job


  • guest Monday, August 13, 2012

    It would be really good if generated setters and getters could appear on a single line. This would really help improve readability.


  • Mark Heckler Tuesday, August 14, 2012

    Great article with great reminders! Diff is #1 for a reason. :-D

    Thank you for bringing up a couple of these I'd forgotten/fallen out of the habit of using!

    All the best,

    Mark


  • Gilberto Friday, August 24, 2012

    Thanks!

    Excelent contribution!


  • guest Thursday, September 6, 2012

    Wow, I didn't know half of them. Very educative.


  • Steven Friday, September 14, 2012

    Thanks a ton for the information. It is really awesome.


  • guest Monday, November 5, 2012

    very helpful... thanks a lot


  • Deepak Bhagat Friday, February 1, 2013

    Very very thanks to u.


  • guest Monday, August 26, 2013

    appreciate the info


  • David Wednesday, March 26, 2014

    This may be know to some but I figured I would mention it. You can add your own Code Templates by following the directions below. I created one some for my logging (lgi = LOG.info, lge = LOG.error, lgw = LOG.warn).

    Go to Tools > Options

    Click Editor option

    Select Code Template tab

    Select New button

    In dialog name your template (e.g. lgi)

    In the Expanded Text tab type your command

    Example for lgi - LOG.info("${cursor}");)

    Now all you have to do is type lgi + tab and you have you log message started.


  • Shrinivas Shukla Tuesday, May 13, 2014

    I think the shortcut given in no. 7 is wrong.

    In NetBeans 6.8 and above, 'Alt+Shift+F' does the code formatting not 'Ctrl+Alt+F'.

    Don't know about the previous versions.


  • Tom McGinn Tuesday, May 13, 2014

    @Shrinivas

    You are absolutely right - the correct key sequence for formatting is ALT-Ctrl-F. Good catch. I'll have to update the blog...

    Tom


  • Shrinivas Shukla Tuesday, May 13, 2014

    @Tom

    Not 'Alt+Ctrl+F' but it is 'Alt+Shift+F'.


  • Tom McGinn Tuesday, May 13, 2014

    @Shrinivas

    Yikes. Yes - Alt-Shift-F. Tom


  • Abah Sunday, July 26, 2015

    wao! thanks!!, this will definitely help me save a lot of time.


  • Arpan Das Thursday, October 29, 2015

    Excellent. Really time saver. Thanks.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.