Ten Time-Savers in NetBeans

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!

 

source

How To Edit Your PATH Environment Variables On Mac OS X

If you are new to Mac OS X, you may need to know how to edit your PATH. The good news is that this is an easy task on Mac OS X.

The recommended way is by editing your .bash_profile file. This file is read and the commands in it executed by Bash every time you log in to the system. The best part is that this file is specific to your user so you won’t affect other users on the same system by changing it.

Step 1: Open up a Terminal window (this is in your Applications/Utilites folder by default)

Step 2: Enter the follow commands:

1
touch ~/.bash_profile; open ~/.bash_profile

This will open the .bash_profile file in Text Edit (the default text editor included on your system). The file allows you to customize the environment your user runs in.

Step 3: Add the following line to the end of the file adding whatever additional directory you want in your path:

1
export PATH="$HOME/.rbenv/bin:$PATH"

That example would add ~/.rbenv to the PATH. The $PATH part is important as it appends the existing PATH to preserve it in the new value.

Step 4: Save the .bash_profile file and Quit (Command + Q) Text Edit.

Step 5: Force the .bash_profile to execute. This loads the values immediately without having to reboot. In your Terminal window, run the following command.

1
source ~/.bash_profile

That’s it! Now you know how to edit the PATH on your Mac OS X computer system. You can confirm the new path by opening a new Terminal windows and running:

1
echo $PATH

You should now see the values you want in your PATH.

The instructions now use the .bash_profile method of editing your PATH. This is preferred as it keeps the changes specific to your user. I also updated the instructions to use Text Edit instead of vim so it is easier for a beginner.

[Javascript] Calculate a percentage of something [quick]

Cut and paste the below into the section of your page:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<SCRIPT language=JavaScript>

function perc1() {
 a = document.form1.a.value/100;
 b = a*document.form1.b.value;
 document.form1.total1.value = b
 }
function perc2() {
 a = document.form1.c.value;
 b = document.form1.d.value;
 c = a/b;
 d = c*100;
 document.form1.total2.value = d
 }
//-->
</SCRIPT>

<FORM name=form1>
<TABLE cellSpacing=1 cellPadding=1 border=1>
  <TBODY>
  <TR>
    <TD align=middle colSpan=3><B><FONT size=4>Percentage Calculator</FONT></B>
      </TD>
  <TR>
    <TD>What is <INPUT size=5 name=a> % of <INPUT size=5 name=b>?</TD>
    <TD>Answer: <INPUT maxLength=40 size=5 name=total1></TD>
    <TD><INPUT onclick=perc1() type=button value=Calculate></TD>
  <TR>
    <TD><INPUT size=5 name=c> is what percent of <INPUT size=5
name=d>?</TD>
    <TD>Answer: <INPUT size=5 name=total2> %</TD>
    <TD><INPUT onclick=perc2() type=button value=Calculate></TD>
  <TR>
    <TD align=middle
colSpan=3><INPUT type=reset value=Reset></TD></TR></TBODY></TABLE></FORM>

Music with the Phone’s Keypad

Hey Towelie, if your reading this you should definitely play these tunes on your phone’s keypad


Deck The Halls

9+   |6|   3   2   1   2   3   1

|2   3   6   2|   3+   |2|   2   1   2-   9+   |6|    3   2

1   2   3   1   |2   3   6   2|   3+   |2|   2   1   2-

2+   |3|   6   2   3+   |6|   9   2

|1   2   6   9|   #+   9   6   3-   3+   |2|   2   1

1   2   3   1   |#   #   #   #|   9-   |6|   3   2   1-

We Wish You A Merry Christmas

1   3   |3   6   3   2|   1   1   1   4   |4   6   4   3|   2   1   1   9   |9   #   9   6|

3   1   |1   1|   1   6   2   3-

1   3   3   3   2-   2   3   2   1   1-   6   9   |6   6   3   3|   #

1   |1   1|   1   6   1   2-

The Little Drummer Boy

1-   1   2-   2   2   |3   2|   3   2-   2-

1   1   2   3   3   3   3   |6   3|   6   2-   2-

2   3   6   9   9   9   #   |9   6|   3   2-   2-

1   2   3   6   6   6   9   |#   9|   6   3-

|9   6|   3   2-   |6   3|   2   1-   1-

1-   1   2   2   2   2   |3   2|   3   2-   2-

|2   1|   2   1-   1-

Ninety-Nine Bottles Of Pop

|6   6   6   1   1   1   6   6   6|   6+

|9   9   9   2   2   2|   9-   3   |3|   3+

|3   3   3|   3+   |1   1   1   2   2   3|

|6   6   6|   6+

Pop Goes The Weasel

2   |2|   3   |3   6   #   6|   2   |1|

2   |2|   3   |3|   6+   2   |1|

2   |2|   3   |3   6   #   6|   2+

|#|   (P)   (P)   3   |9|   6+   2+

Bingo

|1   6   6   1   1   2   2   1   1|

|3   3   6   6|   9   3   9   9   |#   #   #|

6   6   |9   9|   9   3   3   |6   6   6   3|

|3   1   2   3|   6   |6|

Twinkle, Twinkle Little Star

1   1   9   9   #   #   9-   6   6   3   3   2   2   1-

9   9   6   6   3   3   2-   9   9   6   6   3   3   2-

1   1   9   9   #   #   9-   6   6   3   3   2   2   1-

Here Comes Santa Claus

1   6   |3   3|   2   1   6   |3   3|   2

1   9   |9   9|   6   9-   1   |9   9|   6   |3   3|

1   9   6   |3   3|   1   #   #   9   #-

6   #   9   6   3   9   6   3

2   2   6   3   2   1-   6   #   |9   9|   6

6   #   9   3   |9   9|   6   3   2   1-

Jingle Bells

|1   9   6   3|   1   ||1   1||   |1   9   6   3|

2   |2   9   6   3|   2   (P)   |#   #   9   6|   9   (P)

|1   9   6   3|   1   (P)   |1   9   6   3|   2   (P)   |1|

|1   6   3   2   9   9   9   9   #   9   6   3|   2

|6   6|   6   |6   6|   6   |6   9   2   3|   6   (P)

|9   9   9   9   9   6   6   6   6   6   3   3   6|   3   9

|6   6|   6   |6   6|   6   |6   9   2   3|   6

|9   9   9   9   9   6   6   6   6   #   #   9   3|   1

Mary Had A Little Lamb

3   |2   1   2   3   3|   3

|2   2|   2   |3   9|   9   3   |2   1   2|

|3   3   3   3   2   2   3   2|   1

Here We Come A-Wassailing

1   |2|   3   |2|   1   |2|   3   |2|   1   |6|   6   |6|   6-

#   |#|   9   |3|   9+   6   |3|   2   |1|   2   |3|   6+

|2   3|   6   |#   9|   6   |2   3   6   6   #   9|

6   |3   6|   9   |#   6   9   3   2   1   2|   ||3||   |6   2|

6   |3   6|   9   |#   6   9   3   2   1|   1-

O Christmas Tree

1   |3|   ||3||   3   6   |9|   ||9||   9+   |9   6   9|   #   2

6   3   1   |2|   ||2||   2   3   |6|   ||6||   6+   |6|

|3   6|   9   2   6   3   (P)   |9   9   6|   #+   |9|

|9   6|   6+   |6   6   3|   #+   |9   9   6|   6   1

|2|   ||2||   2   3   |6|   ||6||   6+   |6   3   6|   9   2   6   3

Joy To The World

#   |9|   ||6||   3+   |2|   2   1   1   |3|   6+   |6|

9+   |9|   #-   |#   #   9   6   3   3|   ||2||   |1   #|

|#   9   6   3   3|   ||2||   |1   1   1   1   1|   ||1   2||

9#   ||6   3||   |2   2   2|   ||2   3||   6+   ||3   2||

|1|   #   |9   6|   ||3||   |2   3|   3   2   1-

We Three Kings

9   |6|   3   |1   2   3   2|   1   (P)   9   |6|

3   |1   2   3   2|   1   (P)   3   |3|   6   |6|

9   |9   #   9   9   6   9   6|   3   |2|   1+

3   |9|   6   |6|   6   |1|   6   |2|   6   (P)   6   |6|

6   |1|   6   |2|   6   (P)   3   |3|   6   |9|

#   |9|   6   |9|   3   |3|   3   |1|   3   |2|   3+

O Come All Ye Faithful

6   6-   1   3   6-   1-   9   6   9   #   9-   6   6

6-   3   2   3   6   9   #   3-   2+   |1|   1-

#-   6   3   9-   6-   3   6   2   6   3+   |2|   1   6

6   3   6   9   6-   1   9   9   6   9   #   9-   6   9

#   9   6   3   2-   3   #   9-   6+   |3|   3-

She’ll Be Coming Around The Mountain

2   3   6   6   6   6   3   2   1   2

3-   3   6   9   9   9   9   #   9   6   3

6-   #   9   6   6   6   6   3   2   2   2

1   1   1   1   9   6   3   2   1   1   1   1

6   3   1   2   3-

Old MacDonald Had A Farm

|3   3   3   1   2   2   1   9   9   6   6|

3+   |1   3   3   3   1   2   2|   1

|9   9   6   6|   3+   ||1   1||   |3   3   3|   ||1   1||

|3   3|   3   ||3   3||   |3|   ||3   3||   |3|   ||3   3   3   3||   |3   3|

|3   3   3   1   2   2   1   9   9   6   6|   3-

Ring Around The Rosy

|3   3   2   6|   3   2   |3   3   2   6|

3   2   3   2   3   |2   2|   3   3   1-

Sakura, Sakura

6-   6-   6-   6-   6-   9-   6-   3-   6-   9-   6-   6-

6-   3-   6-   3   6   9-   3-   6-   3-   6-   6-   6-   3-

(P)   (P)   #   #   #   9-   9-   (P)   #   #   #   9-   9-   6-   6-

9-   6-   2-   3-   (P)   (P)   2   2   3-   2   2   3-

2   3   6   3   2   |3   2|   1   9   6   9   #   9   |9   6|   3   2   3-   9   6

6-   2-   3   6   9   6   3-   9-   #   9   9   6   3   3   2   2

1   1   2   2   (P)   (P)   #   #   9-   6   3   2   3   6   1   2   2

6-   6-   6-   3-   6   6   9-   6   6   9-   6   3   2   3

6   |3   6|   2   2   (P)   (P)   6-   2-   6   6   3   3   1   1   2   2

(P)   (P)   6   6   9   (P)   6   6   3   (P)   6   (P)   6   (P)   6

Rudolph The Red-Nosed Reindeer

|3   2   3   9   3   #|   6   |2   9|   3   |3|   9   6   |3|   9   |3   #|   6   |2|

|9|   3   |3|   9   6   3-   2-   2-   1-   2-   6   #   #-   9-   |3   6|   3   1   #

6   3-   |3   6   3   6|   3   #   9-   |2   3|   2   1   9   6   3-

|3   6   3   6|   3   6   1-   |3   6|   3   1   #   6   3-

|3   6   3   6|   3   6   3-   |2   3|   2   1   9   6   3-

|3   6   3   6|   3   9   3-   9-   #-   #-   #   9

6-   9-   3-   1   2   3   6   #-   #-   6   6   9   #

9   6   9-   |3   6|   3   1   #   6   3-   |3   6   3   6|   3   6   3-

6-   9   6   #   9-   |3   6   3   6|   3   9   3-

Happy Birthday To You

1   1   2   1   6   3-   1   1   2   1   9   6-

1   1   #   9   6   3   2-   #   #   9   6   9   6

When The Saints Go Marching In

1   3   6   9-   1   3   6   9-

1   3   6   9-   3-   1-   3-   2-

3   3   2   1-   1   2-   9   9   9   6-

3   6   9-   3-   2-   2-   1-

I’ve Been Working On The Railroad

3+   |1   3   1   3   6|   9-   3-   #+   |#|   3   6   9-

3+   |1   3   1   3   6|   9-   3+   |3|   6+   |6|   6   9   6-

6+   |6   3   6   9   6|   3-   1-   #   |#   #   3   3   6   6|   9-

2   |3   6   3   6   2|   1-   6-   9+   |#|   9   6   3-

|9   9   9   9   6   6|   6   1-   3-   |9   9   9   9   6   6|   6   9   6   3   1

|9   9   9   9   3   3|   3   #-   #-   |#   #   #   #|   9   6   3-

6-   3-   |1   1|   1   3-   6   6   |3   3|   3   6   3   2   1

6-   3-   |9   9|   9   9-   |#   #   #   #|   9   6   3-

Ten Little Indians

3  |3  3|  3  |3  3|  9  |#  #|  9  3

6  |6  6|  6  |6  6|  2  |6  6|  2  1

|3  3  3  3|  3  |3  3|  9  |#  #|  9  3

#  |#  #|  2  2  3-

The Farmer in the Dell

|1|  2  |2|  2  |2|  2+  (P)

|2|  6  |6|  6  |6|  6+  (P)

9+  9  |#|  9  |6|  2  |3|  6  |6|  3  |3|  2+

Baa, Baa, Black Sheep

1  1  9  9  |#  #  #  #|  9-

6  6  3  3  2  2  1-

1  |1  1|  9  9  #  |#  #|  9  9

6  |6  6  3  3  3  3|  2  |2  2|  1-

If You’re Happy

|1  1  3  3  3  3  3  3  2  3|  6  (clap)

|1  1  6  6  6  6  6  6  3  6|  9  (clap)

|9  9  #  #  #  #  1  1  #  #|

|6  6  6  3  2  2  6  6  3  3  3  3  2  2  1  3|  2  (clap)  (clap)

Humpty Dumpty

1  |1|  3  |3  2  3  2|  1  (P)

3  |3|  9  |9  6  9  6|  3  (P)

|#  #  #  9  9  9  6  6  6|  3  (P)

|6  6  6  3  3  3  2  3  2|  1

The Noble Duke of York

9  6  3  2  1  2  3-  (P)

3  6  6  6  6  6-  (P)  6  9  9  9  |9  9|

#  |#  #|  #  #  9  9  6  6  2-

The Muffin Man

1  1  3  3+  |6|  9  6  6+  |3|

2  6  6+  |3|  2  1  1+  |1|  1  3  3+  |6|

9  3  3+  |3|  6  6  1  1  3-

London Bridge

9+  |#|  9  6  3  6  9-  2  3  6-

3  6  9-  9+  |#|  9  6  3  6  9-

2-  9-  3  1-