
490
|
Chapter 12, Miscellany
#97 Mirror an Application
HACK
Put all of this together, with the following main( ) method, and then fire up
two copies of your program. The first one will wait for a connection. When
the second one starts, it will send every mouse event over the network to the
first copy, which will then reuse it. If you click on the button in the second
window, you will see the button depress in the first.
public static void main(String[] args) throws Exception {
ApplicationMirrorTest mirror = new ApplicationMirrorTest( );
mirror.start( );
}
Component Problems
Wait...did this work? No, it didn’t. The events still don’t work after being
sent over the network. A little debugging will show that every part of the
reconstituted event works properly except for the
getComponent( ) method,
which returns
null. Why?
The reference to the component doesn’t get sent over the wire because that
would require sending the component itself. That component, of course, is
part of your entire Swing tree, which would also have to be sent over. Pretty
soon you’d be sending a few megabytes through the network for every event.
To avoid this, the developers of Java made the component reference tran-
sient, which means the object will be skipped during serialization. That
makes the component fast, but it presents a problem: how do you know
which component the event goes with?
When you think about it, you wouldn’t