Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline
Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Subscription to Java Pro


Serialize Java Data Objects to XML (Continued)

Three Strategies
Moving complete GUI designs between development tools seems best served by using the Java source code rather than an intermediate form like XML. There are two main persistence issues for saving GUIs: event handlers and default property values such as layout managers. The persistence mechanism can access only public methods to restore an object. Therefore, inner classes or nonpublic classes, often used by GUI IDE tools, for event handlers cannot be saved and restored. There are at least three strategies to use for event handlers: use a public ActionBean that implements an ActionListener approach; use the new JDK 1.4 java.beans.EventHandler.create(..) method to make a dynamic proxy; and don't save event handlers, but instead set them after loading the JPanel by iterating over the JPanel's components. For large real-world designs, the latter may be the best choice.

ADVERTISEMENT

Listing 8 implements an ultrasimple fast food menu GUI with a JPanel holding food order buttons. This small example demonstrates all three of the event handler strategies. The ActionBean is:

package xmlpersist;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ActionBean 
   implements ActionListener {
   public void actionPerformed
      (ActionEvent e) {
      System.out.println(
         "button pressed , cmd = " 
         + e.getActionCommand() );
   }
}

The dynamic proxy method needs a target object. A "thing" object is the target; it will be instantiated when the panel is restored because it is present in the XML. You shouldn't use the frame itself—that is, "this" as the target—or it would make a new frame every time you restored a panel:

package xmlpersist;

public class Thing {
   private static int thingCount=0;
      public Thing() {thingCount++;}

      public void orderSomething(
         String what) {
            System.out.println(
               "thing #"
               + thingCount + 
               "  orderSomething = "
               + what);
   }
}


Back to top

Printer-Friendly Version












Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home