Screencast: Developing Swing with JavaRebel

June 12th, 2008 by Jevgeni Kabanov
We prepared a short screencast that demonstrates how easy it is to develop Swing applications with JavaRebel. We used the simple Metalworks example (a multiwindow mail client mockup) that comes with the Java SDK and played around with adding fields and buttons.

We start by launching the application with “-noverify -javaagent:javarebel.jar” added to the VM params in the Eclipse launch profile. We first add new field “BCC”, which immediately appears in a new window. We then also add a Send button, which first does nothing. We then proceed to add a listener that points to the new send() method. After we make the body of the method to show a message box it is visible in both new and old windows that had a listener added. Finally we refactor the class adding a field and showing its value in the message box. Note that old instances have the field, but it’s initialized to null, while the new instance works properly.

Since most of the Swing initialization will happen only once, a nice trick we found for making a component reinit itself is using the JavaRebel SDK as follows:

JAVA:
  1. ReloaderFactory.getInstance().addClassReloadListener(
  2.     new ClassEventListener() {
  3.   public void onClassEvent(int eventType, Class klass) {
  4.     if (MyClass.class == klass) {
  5.       java.awt.EventQueue.invokeLater(new Runnable() {
  6.         public void run() {
  7.           reinit();
  8.         }
  9.       });
  10.     }
  11.   }
  12. });

  • Name
    I would be interested in what you do in the reinit() method.
    Thanks a lot.
    -Christian
  • I don't know the details of this specific demo but I would assume that in Swing the reinit would contain the reinitialization of GUI elements. Such as constructing buttons, labels, content panes etc.
  • Christian Beil
    Sure, but I think the components must be removed and re-added to the container component, and repainting has to be triggered.
    So, does reinit() contain something like this?
    containerPanel.removeAll();
    init(); // re(construct) buttons, labels, etc. and add them to containerPanel
    containerPanel.revalidate();
    containerPanel.repaint();
blog comments powered by Disqus
Olark Livehelp