I recently worked on some personal projects that required Swing development. They were some desktop applications and some Java applets. For the development, I used primarily Eclipse, because I like the IDE and I am very accustomed with it. However, for the Swing development, I didn’t wanted to write the code by myself. Therefore, I had to either search for a Swing plugin for Eclipse or revert to some other solution (read IDE) for developing the Swing parts. Here is the combination that works for me very well.The first solution was to find a proper plugin for Eclipse to do the Swing layout of elements, editing of properties and the general binding of the interface to the controller. Although I haven’t tested the latest version of the existing Swing editors, a few months ago I remember that pretty much all of the plugins available for Eclipse were unstable and not really to be trusted for a project. Also, the generated code was not something to be proud of (although, as you will see in the next lines, my aim was to never touch or look at the code generated by the Swing editor.
Therefore, I chose to not do the Swing editing in Eclipse. I selected a different IDE, namely Netbeans, for the Swing part. Netbeans comes with a fantastic plugin for Swing programming, formerly called Matisse. Working with the plugin is really easy, you can just drag the components, arrange them how you want, and that’s it.
To connect the interface to my code, I implemented Controller classes. The only customization that I added to the Swing forms was a reference to the corresponding controller and the wiring to the controller methods in the listeners for the buttons, menus, etc.
Here is how such a Swing dialog would look like:
public class FolderDialog extends javax.swing.JDialog {
private static final long serialVersionUID = 1L;
private FolderDialogController controller;
/**
* Sets the controller to the given value. See {@link #controller} for more details.
*
* @param controller
* the new value for controller
*/
public void setController( FolderDialogController controller ) {
this.controller = controller;
}
// ----------------------------------------------------------------------
/** Creates new form FolderDialog */
public FolderDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
}
........
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
controller.handleOkClick();
}//GEN-LAST:event_jButton2ActionPerformed
........
}
The reference to the controller, the setter for it and the invocation of controller’s methods were the only changes that I performed in the generated code. Otherwise, all the changes were operated using Netbeans.
I found that it was quite easy to change between IDEs, not to mention that Netbeans is REALLY fast and smart at detecting changes in the project and reloading them (for example it detects the changes in the Eclipse projects, and reloads the classpath, the libraries, and various other settings).
Usually I am against the generators of code. I don’t trust them and I think that they are not able to generate the code as easy to read and as perfect as I am able to write it. Therefore, I try to avoid them as much as possible. That is why I never use JSP/JSF editors that provide drag & drop features, like those available in VS for ASP.NET. However, for Swing development I found that Netbeans was a time saver and if it hadn’t been for me to favor Eclipse, I would have developed both of the Java desktop projects in Oracle’s IDE.







