NetBeans IDE 5.0 Hacks (part 2)
A collection of the best tech tips from NetBeans blogs
Use a Single Editor Well - The editor should be an extension of your hand;
make sure your editor is configurable, extensible, and programmable.
-- The Pragmatic Programmer
See also NetBeans IDE Hacks table of contents
Project Hacks
GUI Builder Hacks
Look and Feel Hacks
Project Hacks
Save a NetBeans Project or File Under a New Name
source
To save a project under a new name, right-click the project node
and select Copy Project from the context menu. Enter the new name
and path into the dialog, and press Copy.
To save a file under an new name, select the file and press Ctrl-C to copy.
Then select the directory where you want the copy to go, and press Ctrl-V to paste the file.
A new copy of the file appears.
To rename the file, click the file label and enter the new name.
As soon as you hit return,
a Refactoring dialogue will pop up and offer you
to rename all occurences of the name — press Next to do so.
Depend on Other Projects
source
If your project gets big enough, it is a good idea to divide it into smaller projects.
Splitting up a huge project lets you structure your code better by separating
logical parts into individual projects.
Especially if you work on one big project in a team,
it's handy to have the projects divided
because people can work on each of them separately.
However, if you split up the project, you need to tell the main project
how to access classes from the other project parts.
- For Java SE projects, right-click the main project and choose Properties.
- Go to the Libraries section and choose Add Project.
- Browse to the project folder on your disk.
Select it to add the project's JAR to the compile classpath:
When you next compile the main project, the status of all dependent projects is checked,
and any updated classes they contain are compiled as well.
The only drawback of dependent projects is that they compile more slowly
if the projects are divided up too much and have too many dependencies.
This is because the IDE has to check the status of all dependent projects.
If you experience a slow-down, set up your project to not always build
dependent projects automatically.
Unchecking the Build Projects on Classpath checkbox
in the Libraries section of the main project's Properties
will decrease compile time, but remember,
you will need to rebuild dependent projects manually every time they change.

Note that for NetBeans plug-in projects, the dependence mechanism is quite different:
Either you depend on one of the NetBeans modules, or you define dependence on an external library.
Generate Javadoc from JDK Sources
source,
source2.
You can build the the whole JDK javadoc from the JDK's sources.
This way you get the javadoc without having to download anything extra.
The magical options can be found on
java.sun.com.
javadoc
-J-Xmx500M
-d docs
-use
-splitIndex
-windowtitle 'Java 2 Platform 5.0 API Specification'
-doctitle 'Java<sup><font size="-2">TM</font></sup> 2
Platform 5.0 API Specification'
-header '<b>Java 2 Platform </b><br><font
size="-1">5.0</font>'
-bottom '<font size="-1"><a href="http://java.sun.com/cgi-bin/bugreport.cgi">Submit
a bug or feature</a><br><br>Java is a trademark or registered trademark
of Sun Microsystems, Inc. in the US and other countries.<br>
Copyright 1993-2004 Sun Microsystems, Inc. 901 San Antonio Road,<br>
Palo Alto, California, 94303, U.S.A. All Rights Reserved.</font>'
-group "Core Packages" "java.*"
-overview /home/src/overview.html
-sourcepath /home/src
java.applet java.lang
This is meant to be written all in one line at the command prompt.
On the last line, you list explicitly all packages from which you want to build javadocs -
note that wildcards don't work here.
One small tradeoff is the speed of building it, it takes a while.
A second caveat is that some of the javadocs are missing for classes that don't have sources (security, etc.).
Navigate to Compile Errors Quickly
source
Sometimes, you just want to see the big picture.
Will this class compile ot not?
If not, where does the bug start?
And — did I place the breakpoint before or after that spot?
You can gain this type of information with one glance from the Error stripe.
The error stripe is the area on the right side of the Editor. It displays
the current cursor position, and a colored mark for each warning or breakpoint.
A little colored box at the top tells you immediately whether this class will
compile (green) or not (red). If it's red, and there are marks in the errorstripe,
clicking a mark will take you directly to the error.
Access Files Outside Projects
source
From time to time it is useful to have access to files
which are not a part of a NetBeans project.
For instance, many people use NetBeans IDE to update HTML pages on their website.
Obviously, these files are not a part of a project.
To view and edit these files, use the Favorites view.

Favorites View
Favorites view is not visible by default in the Explorer.
You can open it from the Window menu, or by pressing Ctrl-3.
The root of a new favorite directory is added by right-clicking in the Favorites view.
These roots are remembered across sessions,
so you need to add them only once (unless you change the userdir).
Use a Different Target JDK
source
If you want the IDE to produce compiled .class files
in a version that is lower than the JDK you're running,
you need to set a different target JDK.
This can be useful for instance if the customer you are writing code for
is using an older version of Java.
You can still use JDK 5 as your development platform,
but by setting the source level, you specify the Java version for the compiler.
- You specify the target JDK by right-clicking the Libraries node in Projects View and choosing Properties.
- Select the Sources section.
- In the Source Level combobox,
you can choose source level 1.2, 1.3, 1.4 or 1.5 (if your platform is JDK 5).
If you for instance set the source level to 1.4,
the compiler won't accept all the new JDK 5 features
like varargs, generics, autoboxing, enhanced for loops, etc.
These are all marked as errors. When you compile your project,
all classes are created with the source level you specify in here.
It is possible to set the source level for freeform projects, too,
but it requires some more steps:
For details see page 17 in chapter 12 of the NetBeans IDE Field Guide.
You have probably noticed that JDK 1.5 is now called JDK 5,
and JDK 1.6 is refered to as JDK 6. Read more in this article:
"Version 1.5.0 or 5.0?".
Integrate NetBeans API Javadoc and Sources into the Editor
source
Installing the NetBeans API Javadoc in the IDE is as simple as
going to the Update Center and selecting 'API Documentation':

It might take you a while to download the API Documentation module.
But then working with the APIs is much easier
Among other things, the Javadoc even includes info about the entries required in the layer.xml file.

By the way, if you download the
netbeans-5_0-beta-src-ide_sources.zip,
your world becomes even better: Add the ZIP file to the Sources tab in the NetBeans Platform Manager...

...and you'll not only be able to read the Javadoc, but you'll also be able to jump to the actual source code:

Alternatively, just hold down the Ctrl key move your mouse over
any identifier. If it has sources, it gets a hyperlink that you can click on to jump there:

Either way, you'll then be able to read the sources of the selected piece of code:

Automate Tasks With Ant
source
Every night, you create a zipped and tarred backup copy of your project's sources.
And every night, you copy your project's newly-built JAR file to a network share
to publish the bleeding-edge development build of your application.
Why do it manually? Automate these tasks with NetBeans and Ant.
NetBeans utilizes Ant as its primary project engine: Everytime you build a project, the IDE executes Ant,
the de-facto a standard for building Java applications.
Similar to GNU make, Ant is a tool to automate the build process.
Ant, too, can handle build dependencies, but unlike make,
Ant is completely platform- and shell-independent.
The automated build procress can be extended easily, since Ant build files are written in XML.
To extend the build process,
you override the build.xml file which is stored in the main directory of the project.
By default, the build.xml file just includes a file called build-impl.xml,
located in nbproject subdirectory.
The file build-impl.xml should not be touched (it is generated automatically).
Instead, you override empty default targets
by adding the following lines to your build.xml file:
<target name="-post-compile">
<tstamp/>
<tar tarfile="${dist.dir}/sources.tar" basedir="${src.dir}"/>
<gzip zipfile="${dist.dir}/sources.tar.gz" src="${dist.dir}/sources.tar"/>
<copy file="${dist.dir}/sources.tar.gz"
tofile="h:\shared\myapplication\${DSTAMP}\myapplication-sources-${DSTAMP}.tar.gz"/>
<delete file="${dist.dir}/sources.tar"/>
<delete file="${dist.dir}/sources.tar.gz"/>
</target>
To create an archive of your sources,
you override the "-post-compile" target
to execute tar and gzip tasks.
Tou tell Ant to copy the archive into a directory,
and to label both the archive and the directory with a timestamp.
In order to use a timestamp, you initialize the DSTAMP variable using a task called <tstamp/>.
<target name="-post-jar">
<copy file="${dist.jar}/"
tofile="h:\shared\myapplication\${DSTAMP}\myapplication-${DSTAMP}.jar"/>
</target>
To copy the JAR from the dist directory (where it is created) to
the network drive, you override the "-post-jar" target.
You reuse the timestamp created above.
From now on, every time you build the project, a copy of the JAR and an archive of the sources
are created in a directory with the current timestamp on your shared network drive.
When you build the main project, you get the following output:
Don't forgot to read our answers to the most Frequently Asked Questions About Ant and NetBeans: NetBeans and Ant can help you automate a lot of tasks you may be doing manually.
You can learn more about Ant in general in the Ant user manual:
Make sure to take a look at the overview of
tasks.
Define Shortcuts for Ant Targets
source
It's easy to define a keyboard shortcut for an Ant target that you use a lot.
- In the Files view, edit
build.xml and type in your custom ant target.
- While
build.xml is open, you can see the list of all targets in the Navigator.
Right-click on any of these targets to define a shorcut for it:

When you invoke the shortcut, the corresponding Ant target is executed.
Now you can take advantage of Ant's power by pressing a single key combination.
- Similarly, you can add a menu item or a toolbar button for the Ant target:

Tight integration of NetBeans with Ant is a great thing, notice how many things are possible:
Geertjan has a huge collection of tips how to use Ant integration in his blog.
Sweep End-of-Line Characters
source
The list of possible
Ant tasks
opens up lots of opportunities. For instance,
developers working on another platform such as Linux or MacOS
may want their sources to always have Windows line endings.
Here is an easy way to achieve that:
Add the following line to the build script's post-compile target:
<target name="-post-compile">
<fixcrlf srcdir="${src.dir}" eol="crlf"/>
</target>
Whenever the project is built, all line endings are converted.
Note that NetBeans preserves the line endings in the Editor,
so it's not necessary to run the End-of-Line Sweeper every time.
If you have many classes and don't want to run the the Sweeper for every build,
you can instead create a custom Ant target that you only call when you need it.
<target name="EOL sweeper">
<fixcrlf srcdir="src" eol="crlf"/>
</target>
The target is automatically added to the context menu:

Adding a custom build target to the Run Target > Other Targets context-menu.
GUI Builder Hacks
Design GridBag Layouts Easily
source, Tim Boudreau
The NetBeans GUI Builder Matisse is not only the ideal choice for creating
GUIs with the new GroupLayout Manager,
it also assists you with another common layout — GridBag.
Create a new project and add a new JPanel to it.
When the JPanel comes up in the GUI Builder's Design view,
right-click it and select Set Layout > GridBagLayout.
Choose some GUI elements from the Palette (Ctrl-Shift-8) such as buttons and drop them into the JPanel.
Right-click the JPanel and select Customize Layout from the context menu
to open the GridBag Customizer.

Right-Click a GridBag GUI to open the GridBag Customizer window.
The GridBag Customizer lets you directly edit all the properties of a GridBag Layout:
You can rearrange the position of GUI elements and assign values to insets, padding, weights,
grid size and anchoring for each component.

The GridBag Customizer window in the NetBeans GUI Builder.
If you create a GUI with rows and columns that fits the grid scheme,
go for it and try the GridBag Customizer.
Add Multiple GUI Components Fast
source
When creating GUIs in the Editor's Design view, you often find yourself
going to and from the Palette to select the same kind of element repeatedly.
You can speed up the process and use the shift key to place
multiple components of the same kind into the form.
- Click to select the GUI component in the Palette.
- Then move the mouse over to the JPanel. An instance of component will follow.
- Hold down the shift key, and click to place each component.
- Release the shift key right before placing the last component.

You can see a demo of this method applied in Roman's
Javalobby expert presentation: Matisse.
Create Tabbed Panes Fast
source
Creating Tabbed Panes is not a very intuitive process.
Let's see how the NetBeans GUI Builder can assist you to
create Tabbed Panes easily and quickly:
- Drag a JTabbedPane from the Component Palette onto the Design View:
- Drag a JPanel onto the JTabbedPane — the first tab appears:
- Drag another JPanel onto the tab's label (!) until you see the orange dashed outline (!) pictured below:
- Then drop it. You'll see the second tab:
- If you want to use more than two tabs,
click one of the labels of the JTabbedPane to get handles that let you resize the component.
- Drop JPanels onto the tab's label until you have the number of tabs you need.

As you can see, in order to create new tabs, it's important to drag the JPanels
onto a tab's label, and not into the tab's content area:
You know you got it right when the dashed orange outline appears.
After you created the number of tabs that you need, you can add components like buttons or text fields
to the tabs; drag these components onto the tab's content area.
Prototype Rapidly
source
The NetBeans GUI Builder Matisse is the perfect tool for rapid prototyping.
Instead of drawing your UI layout on paper, you just drag and drop components
in the GUI Builder and try different designs.
Test your designs by clicking the Preview Design icon in the Editor bar.
If it's not good enough, just scratch it, you can have another form prepared in a minute.
Following the automatic guidelines speeds up the whole designing process significantly.
Why not use the GUI Builder as a testing ground to design all your GUIs,
even the ones that have nothing to do with Swing?
Look and Feel Hacks
Activate Anti-Aliasing Font Rendering in the Editor
source
NetBeans supports subpixel anti-aliasing in the Editor.
It is however important to note that the best quality is achieved with a DVI connector;
if your LCD screen is connected with an analog connector,
the rendering may look slightly worse than with DVI.
To activate subpixel Anti-Aliasing in the Editor
- Choose Tools > Options from the menu
- From version 5.0 on, click the Advanced Option button now for advanced settings.
- Select Editing > Editor Settings > Text Antialiasing.
- Tip: Add the switch
-J-Dswing.aatext=true into your
netbeans.conf file to anti-alias all fonts.
You need to use JDK 5 or higher to be able to see smoother fonts.
Change the Font Size
source
You can change the Editor's font size, annotation and syntax highlighting behaviour
by selecting Tools > Options > Fonts & Colors from the menu.
To change the IDE's font size globally, including menus and dialogs, use a start-up option.
For example, to set the font size to 18, you would start the IDE with the following command:
netbeans.exe --fontsize 18
For comparison: The IDE's default font size is 11.
To change the size only temporarily, set this start-up option in the commandline when launching the IDE.
For a permanent change, put it into your netbeans.conf file.
Place the option as the last parameter into the netbeans_default_options variable:
# options used by netbeans launcher by default, can be overridden by explicit
# command line switches
netbeans_default_options="-J-Xms32m -J-Xmx128m -J-XX:PermSize=32m -J-XX:MaxPe
rmSize=160m -J-ea -J-Dapple.laf.useScreenMenuBar=true --fontsize 18"
Changing the font size is not only useful for the visually impaired.
Using a bigger font is also advantageous when you use the IDE for live presentations,
for example when demoing your application with a projector.
Switch to a Different Look And Feel
source 1,
source 2,
source 3
If you like, you can run NetBeans with a custom look and feel.
First make sure that the JAR with the custom Look and Feel classes is in the classpath.
To add the JAR with Look and Feel classes to the classpath, use the --cp:p your-laf.jar
option.
Then specify the --laf option when starting the IDE,
either temporarily on the command line, or permanently in the netbeans.conf file.
(see example below)
Alternatively, to force the IDE to use the Java cross-platform Look and Feel ("Metal/Ocean"),
specify javax.swing.plaf.metal.MetalLookAndFeel as the Look and feel class.
Look at the class's documentation -- usually you can supply further options to make it look more spiffy.
You can try it out with the metal look and feel that is already provided in your Java installation.
Change the options line in your netbeans.conf file to look like the following example.
(Note: You have to write the options all in one line!)
# options used by netbeans launcher by default, can be overridden by explicit
# command line switches
netbeans_default_options="-J-Xms32m -J-Xmx128m -J-XX:PermSize=32m -J-XX:MaxPermS
ize=96m -J-Xverify:none -J-Dapple.laf.useScreenMenuBar=true
--laf javax.swing.plaf.metal.MetalLookAndFeel --fontsize 14 -J-Dswing.aatext=true
-J-Dswing.metalTheme=steel -J-Dswing.plaf.metal.controlFont=Dialog-plain-14"
Note that the IDE is not being regularly tested with all alternate look and feel implementations.
Your mileage may vary.