[prog] NetBeans custom code generation (was: Java/Swing and antialiased fonts)

Almut Behrens almut-behrens at gmx.net
Sat Apr 3 20:56:24 EST 2004

On Fri, Apr 02, 2004 at 05:56:01PM +0200, Riccarda Cassini wrote:
> In the meantime, I've found out that in NetBeans I can add/substitute
> custom instantiation code for an *individual* widget (in the form
> editor, via tab "Code Generation", field "Custom Creation Code").
> However, this is quite a daunting task for an existing app with
> hundreds of widgets... Coming from the Windows world, I'm used to
> lots of clicking - still... ;-)

Hi, Riccarda,

did a little experimenting... As it looks, there is a way to save you
from all the clicking.

The custom code you enter in the IDE eventually ends up in an XML tag
in the respective .form file. (You probably realised that there is a
separate .form file for every class managed via the GUI.  This is
holding all the info the IDE needs in addition to the plain sourcecode
in the .java file.)

The relevant section in the file roughly looks like (for every widget):

  <Component class="javax.swing.JLabel" name="jLabel1">
      <Property name="text" type="java.lang.String" value="jLabel1"/>
*   <AuxValues>
*     <AuxValue name="JavaCodeGenerator_CreateCodeCustom" type="java.lang.String" value="new AA.JLabel;"/>
*   </AuxValues>
      <Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription">
        <BorderConstraints direction="Center"/>

The lines marked with '*' only exist, if you've entered custom creation
code. Adding those lines outside of the IDE has the same effect - which
lets you do it automatically.

A quick-n-dirty approach would be to write some script making the
necessary insertions. As a perl-grrl, I'd come up with something like:

#!/usr/bin/perl -i
              # -i = inplace editing
undef $/;
$xml = <>;    # read in whole file

$auxvalues = q{
        <AuxValue name="JavaCodeGenerator_CreateCodeCustom" type="java.lang.String" value="new AA.#WIDGET();"/>

$xml =~ s|(<Component\ class="javax.swing.     # start string (fixed)
          (J[^"]+)               # extract the widget name
          ".*?</Properties>)     # anything up to the point after which
                                 # we want to insert the <AuxValues> tag
         |$1.auxval($2)          # the replacement string, consisting of
                                 # the whole matching substring plus the
                                 # dynamically evaluated $auxvalues
         |xgse;   # options: x = extended regex syntax (allows comments)
                  #          g = apply globally (i.e. repeatedly)
                  #          s = multiline matching
                  #          e = evaluate replacement string as perlcode

print $xml;   # write out file

sub auxval {
    my $widget = shift;
    my $s = $auxvalues;
    $s =~ s/#WIDGET/$widget/;    # set current widget name
    return $s;


This is making certain assumptions about the structure of the XML file.
I'm not sure if they'll always apply.

Well, if you like, you could try this out with your code and see how
far you get. Maybe it does its job already.
(But do MAKE A BACKUP of your source tree (cp -R) before you go playing
around! Else, don't blame me... :)

You can run the script on your whole source tree with a single command:

find /toplevel/of/src -name "*.form" -exec script.pl {} \;

A somewhat cleaner approach would be to use some XML library to read in
the file and create a DOM (Document Object Model) representation of it.
You'd then add the <AuxValues> tags to the respective nodes, and write
the whole modified DOM tree back out into the .form file.

This would get a bit more involved, though. If interested, I'd suggest
you contact me off-list to discuss the details.


More information about the Programming mailing list