Blog

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]
ReloaderFactory.getInstance().addClassReloadListener(
new ClassEventListener() {
public void onClassEvent(int eventType, Class klass) {
if (MyClass.class == klass) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
reinit();
}
});
}
}
});
[/java]

Jevgeni Kabanov

Jevgeni Kabanov is the founder and CTO of ZeroTurnaround (www.zeroturnaround.com), a development tools company that focuses on productivity. Jevgeni has been speaking at international conferences for over 5 years, including JavaPolis/Devoxx, JavaZone, JAOO, QCon, TSSJS, JFokus and so on. He also has an active research interest in programming languages, types and virtual machines, publishing several papers on topics ranging from category theoretical notions to typesafe Java DSLs. You can follow Jevgeni on Twitter as @ekabanov.

WebsiteTwitterMore Posts

  • Pingback: Swing links of the week: June 15, 2008 : Pushing Pixels

  • Name

    I would be interested in what you do in the reinit() method.
    Thanks a lot.
    -Christian

  • http://twitter.com/toomasr Toomas Römer

    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();

Join the Rebellion Facebook Twitter RSS feed