Pátek VI 10, 2005

NetBeans Quick Tip #6 - Abbreviations in Editor

NetBeans editor supports so called abbreviations. They can make you really productive, it's enough to type in psfs, press space and the IDE generates for you "public static final String". The full abbreviation list is in Help | Keyboard Shortcuts.

The nice thing is that you can add your own abbreviations. So whenever you find out you're writing something often, you can create an abbreviation and use it instead of typing (all programmers are lazy, aren't they?). An even nicer thing is that there are people willing to share their abbreviations - Michel Graciano from Brazil has sent me a list of his abbreviations. They're more advanced than the IDE offers by default, for instance, you can type in trc and the IDE generates:

try {
} catch (Exception e) {

The | mark denotes placement of cursor. I have added into the list one of my popular abbreviations - psvm, it generates:

public static void main(String[] args) {

You can download the XML file here. Just copy it to your userdir in ~/.netbeans/4.1/config/Editors/text/x-java/abbreviations.xml. In case this file is already there it means you have already created some abbreviations and then you can just paste the tags from the xml file to add new abbreviations.

These are the contents of the abbreviations XML file:

trtry statement
elifelse if
forfor cycle
trctry catch
ededitor fold
rnreturn null
forjfor cycle with j
ifif clause
foritfor with iterator
psvmmain method

Next time I will write about macros (Michel has also a great list of macros).

Středa VI 01, 2005

NetBeans Quick Tip #5 - EOL Sweeper

In the last tip I have shown you how to extend the build process. There are many possibilities how to use ant - take a look at the list of task categories in ant's manual.

Let's say I am working on Linux and I want my sources to always have Windows line endings. Why? So that BFU Windows users can view my source codes in Notepad :-) I can add a single line into the build script (e.g. into the post-compile target):

    <target name="-post-compile">
        <fixcrlf srcdir="${src.dir}" eol="crlf"/>     

Whenever I build my project all line endings of my sources are fixed. Note that NetBeans preserves the line ending settings, so it's not necessary to do it everytime. If you don't want to do this during every build (when having too many classes) you can create your own target, which you will call on demand. It is automatically added into the context menu:

    <target name="EOL sweeper">
        <fixcrlf srcdir="src" eol="crlf"/>     

EOL sweeper
Isn't ant wonderful?

Úterý V 31, 2005

NetBeans Quick Tip #4 - Extending the Build Process

In today's tip I will show you how to extend the NetBeans build process on a simple example.

Some background first - NetBeans utilizes Ant as it's primary project engine. So when you build any project the IDE executes Ant. Other Java IDEs use as primary project systems different engines, developed specially for these IDEs. That was the case with NetBeans 3.x as well but with NetBeans 4.0 it was changed to Ant which is de facto a standard for building Java applications.

Ant is used similarly to GNU make to automate the build process. It can also handle build dependencies but unlike make it is completely plaftorm and shell independent (it's java-based), it's configuration is XML-based and can be extended easily. You can read more about Ant in it's user manual.

Back to the tip - let's say I want to copy the jar file which is built with my project to a network share - for instance to publish it as a bleeding edge development build of the application. I want to create a backup copy of it's sources as well. Such tasks can be automated with NetBeans and Ant very easily.

To do this I need to override the build.xml file which is stored in the main directory of my project. The build.xml file by default just includes a file called build-impl.xml, located in nbproject subdirectory. Build-impl should not be touched (it is generated automatically), instead the targets in build.xml need to be overriden.

So I override the "-post-compile" target to create an archive with my sources using tar and gzip tasks. I want Ant to create a subdirectory called by current date and name the archive according to the date as well. To do that I at first initialize the DSTAMP variable using a task called <tstamp/>. I also override the "-post-jar" target to copy the jar which is created automatically in the dist directory:

   <target name="-post-compile">
        <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"            
        <delete file="${dist.dir}/sources.tar"/>     
        <delete file="${dist.dir}/sources.tar.gz"/>
    <target name="-post-jar">
        <copy file="${dist.jar}/" tofile="h:\\shared\\myapplication\\${DSTAMP}\\myapplication-${DSTAMP}.jar"/>

Once I build the main project I get the following output:

From now on everytime I build the project a copy of the jar and an archive with soures are created in a directory with current date on a network share. To achieve that I only needed to add few lines into the build.xml file (yeah, I know, you need to know which lines to add... ;-) If you didn't play with Ant yet, I suggest to try it, because it can help you automate a lot of tasks you may be doing manually. It's sometimes funny how much time we can give away to avoid doing a manual task - but with Ant this mostly pays off. Really.

Sobota V 28, 2005

NetBeans Quick Tip #3 - Increasing Font Size

Most NetBeans users probably know how to change font size in editor. You can do it through Tools | Options | Editing | Editor Settings | Java Editor | Font Size. But:
  • Did you know you can change the font size in all types of editors using multiselection?
  • Did you know you can also change the font size of all menus, dialogs and other components?
If you know this, congratulations, you are a true mighty user. If not, take a look at the screenshot:

Those who still can't see the text in full detail should visit a doctor.

As shown on the screenshot, you can select more editors by holding the shift key and clicking on them. This way you can change all of the font sizes in various editors.

How to change the general IDE font size? There is a startup option for this: --fontsize <size> (default size is 11). You can put it into the command line when launching IDE. You can also put it into the netbeans.conf file, which is in the /etc subdirectory of NetBeans installation. Just place it as a last parameter into the netbeans_default_options parameter. And that's it for today's quick tip.

Středa V 25, 2005

NetBeans Quick Tip #2 - Generating Getters and Setters

Somebody has asked recently on of the mailling lists how to generate getters and setters in NetBeans. I've mentioned that some people even thought NetBeans doesn't have that functionality! So yes, it exists, only it's disguised as "Encapsulate Fields".

You can invoke the action either from the context menu in editor or from the main menu. In both cases use Refactor | Encapsulate Fields. The following dialog pops up and it lets you create getters and setters for all fields in the class:

The good news is that generating getters and setters will be available through the code completion in next release, which will make this feature much more discoverable:

Neděle V 22, 2005

NetBeans Quick Tip #1 - Setting Target JDK

I would like to start by this post a series of tips for NetBeans 4.1. My goal is to provide a quick solution for questions of the following kind: "I want the IDE to do something but how the heck do I tell it?". I apologize upfront to people who will read the tips and will think that they are just obvious. Hey, NetBeans user base has trippled during past year, so there's a lot of newbies for who it may not be so clear. I'd also like to give more information than you can find in docs, help or FAQ - to give you some background how things work together (or don't). Enough of introduction, here's the first tip.

Setting target JDK

You'll use this tip if you want the IDE to produce compiled .class files in a version that is lower than the JDK you're running. That can be useful for instance if the customer you're 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 compiler.

How to do it? You can specify the target JDK by right-clicking the Libraries node in Projects View and choosing Properties. Then, go to Sources section and the Source Level combobox is what you are looking for. You can choose source level 1.2, 1.3, 1.4 or 1.5 if your platform is JDK 5. Here is a screenshot:

If you set for instance the source level to 1.4, the compiler won't accept all the new JDK 5 features like varargs, generics, autoboxing, enhanced for loop, etc. These are all marked as errors. When you compile your project, all classes are created with the source level you specify in here.

Side note: You have probably mentioned that JDK 1.5 is now called JDK 5. There won't be any 1.6 but we'll have JDK 6. If you want to understand why, take a look at this page. The world would be boring, if things would be simple and predictable all the time, wouldn't it? That's one of the advantages of IT industry, there's always something new ;-)

Roman Strobl


« duben 2014