X

Geertjan's Blog

  • May 16, 2006

Migrating a Swing Application to the NetBeans Platform

Geertjan Wielenga
Product Manager
Tim Boudreau demonstrated yesterday how to migrate a Swing application to the NetBeans Platform. He started with a Hex Editor (which you can find in the NetBeans sources, in contrib). This is what it looks like:

He then created a module suite project, wrapped the JAR file of the Hex Editor in a library wrapper module, and used a wizard to create an Action class that appears in the popup menu of Java files and in the Java editor. Then, he filled out the Action class's performAction method as follows:

protected void performAction(Node[] activatedNodes) {
DataObject dob = (DataObject) activatedNodes[0].getLookup().lookup (DataObject.class);
FileObject fileObject = dob.getPrimaryFile();
File f = FileUtil.toFile (fileObject);
if (f != null && f.isFile()) {
TopComponent tc = new TopComponent (activatedNodes[0].getLookup());
tc.setDisplayName (activatedNodes[0].getDisplayName());
tc.setLayout (new BorderLayout());
try {
tc.add (new HexEditPanel (f), BorderLayout.CENTER);
tc.open();
tc.requestActive();
} catch (FileNotFoundException fe) {
ErrorManager.getDefault().notify (fe);
}
}
}

So, I followed the steps he took (after scribbling down his performAction method while he was talking), tweaked the ui a bit (i.e., removed menu items and toolbars, which you can do via the ui, something which Tim unfortunately didn't show) and then ended up with the following:

So, now that I have the Swing application on the NetBeans Platform, I can begin adding a lot more cool features. (Plus, I can add a splash screen and distribute this application via webstart or as a ZIP file, which includes the application's executable, all generated by the IDE.) I can now fully exploit the NetBeans APIs and continue adding more functionality, a lot of it straight out of the NetBeans Platform box, via reuse and lots of branding of existing platform functionality or functionality from the IDE. (For an example of reused IDE functionality, look at the Files window in the screenshot above. For an example of reused NetBeans Platform functionality, notice the windowing system: two tabs are open. Guess how much programming I had to do to make that happen? Answer: Zero. So, thanks a lot, NetBeans Platform and IDE.) Apart from the snippet of code above, I didn't need to do any coding to do the migration, thanks to all the ui features for module development that NetBeans IDE 5.0 provides.

PS: Is it just me or does the migrated app look MUCH better than the original?

Join the discussion

Comments ( 2 )
  • Tim Boudreau Tuesday, May 16, 2006
    Technically, you want to subclass TopComponent - I'd actually like it if you could use it like this, but it would be better if the default was PERSISTENCE_NEVER for that, and I think for backward compatibility reasons that can't be changed.
  • Alex Lam Tuesday, May 16, 2006

    PS: Is it just me or does the migrated app look MUCH better than the original?

    From the point that multiple files are now supported - literally for free - definitely!

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha