Netbeans file templates

You may have seen that when you create a new class, interface or something with Netbeans, the IDE automatically generates some code for you.

It is possible to customize this code using the Netbeans File Templates.

Under the “Tools” menu there is an option called “Templates”. This dialog let you create and edit every template used in the IDE. Just click on the “Open on Editor” button to edit the desired template inside Netbeans.

Let’s see how the Java/Class template looks like:

<#assign licenseFirst = "/*">
<#assign licensePrefix = " * ">
<#assign licenseLast = " */">
<#include "../Licenses/license-${project.license}.txt">

<#if package?? && package != "">
package ${package};

</#if>
/**
*
* @author ${user}
*/
public class ${name} {

}

If you edit this file and save it, the IDE will be using your new template for every new Java class you create.

Since Netbeans 6.0 you can use the FreeMarker template language in your file templates. This way you can add logic to the templates via directives such as if/elseif/else and loop constructs.

You can also use some predefined variables:

  • ${date} — Inserts the current date, in this format: Feb 16, 2008
  • ${encoding} — Inserts the default encoding, such as: UTF-8
  • ${name} — Inserts the name of the file.
  • ${nameAndExt} — Inserts the name of the file, together with its extension.
  • ${package} — Inserts the name of the package where the file is created.
  • ${time} — Inserts the current time, in this format: 7:37:58 PM
  • ${user} — Inserts the user name.

This is an example of a modified file template for Java classes:

////////////////////////////////////////////////
/// File: ${nameAndExt}
/// Created: ${date}  ${time}
/// Encoding: ${encoding}
////////////////////////////////////////////////

<#assign licenseFirst = "/*">
<#assign licensePrefix = " * ">
<#assign licenseLast = " */">
<#include "../Licenses/license-${project.license}.txt">

<#if package?? && != "">
package ${package};

</#if>
/**
*
* @author ${user}
*/
public class ${name} {

}

Java & Netbeans: Overriding paint to customize GUI components

I found that it’s somewhat tricky to override GUI components methods with Netbeans, because the IDE automatically generates the code needed for the component, and that code cannot be edited (it’s grayed out).

But there is a property, in the “Code” tab of the component properties called “Custom Creation Code”, that let us insert the creation code we need for that component.

For example. Create a new desktop application project and, using the design view, drop a new JPanel inside the main panel. If you inspect the source code that Netbeans has generated, you can see the declaration:

private javax.swing.JPanel jPanel1;

And the initialization for that JPanel:

jPanel1 = new javax.swing.JPanel();

Note that the code after the “=” is called “Creation Code”.

Now, open the properties menu of the JPanel you have just dropped before and click on the “code” tab. Click on the “Custom Creation Code” property.

This property allow us to insert whatever code we need for the creation of the component.

Today we just want to override paint() so we insert this code:

new javax.swing.JPanel()
{
    public void paint(Graphics g)
    {
        super.paint(g);
        ourCustomPaintingMethod(g);
    }
};

If you check the generated code again you can see that Netbeans has changed the creation code:

jPanel1 = new javax.swing.JPanel()
{
    public void paint(Graphics g)
    {
        super.paint(g);
        ourCustomPaintingMethod(g);
    }
};

This way we can override any GUI component method we want 😉

Minimize any application to the system tray in Ubuntu

Today I was googling to figure out how I could minimize any application to the Ubuntu system tray.

I am very used to Outlook and I have recently migrated to Thunderbird, so I was looking for a method to minimize Thunderbird to the system tray.

I found two solutions.

The first one was New Mail Icon, which is an extension for Thunderbird. It works like a charm, putting the Thunderbird icon in the system tray. This icon changes when a new mail is received.

I also found the AllTray application. With this software you can minimaze any application you want to the Ubuntu system tray. Pretty cute!

To install it, open a terminal and type:

sudo apt-get install alltray

Creating an XNA project for both Windows and XBOX platforms

This week I’ve started a new XNA project and I had some troubles trying to configure the solution with Visual Studio, until I figured out how easy it was.

The point was creating a single solution with both Windows and XBOX projects on it. I also want a single shared folder for the sources.

I started creating a blank solution.

Then I added both XBOX and Windows projects to the solution.

To create a shared folder for sources I had to create a folder in the solution.

Then I added the sources contained in that solution folder in both projects as a link.

If you need to add additional files to the sources folder you must add them in the solution folder, and then add them to both projects always as a link.

And that’s all. :mrgreen:

Building an arcade cabinet from scratch

One of my current projects consists in building and old arcade cabinet entirely from scratch.

This would be a long process. I am planning to buy wood panels and cut them off to form the cabinet based on the blueprints I am finishing off.

I have the controls already ordered at Ultimarc, and I am currently preparing the computer I’m going to bundle inside the cabinet.

I have also a 21” CRT TV happily waiting to be dismantled and connected to the cabinet 🙂

For running roms I’m using Mame, of course, apart of some other good emulators for Sega and Nintendo game consoles.

Paired with Mame I’m using Mamewah, which is a Mame front-end that let you browse your rom list with the arcade controls, rather than with a keyboard and a mouse. It let you use other emulators as well, so we can select any rom, from any emulator we have installed, without leaving Mamewah.

In the following posts I will be explaining each step of the construction of the cabinet.

Wish me luck :mrgreen: