Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Pressing a button during main



On Apr 27, 2007, at 4:06 PM, Dean Shavit wrote:

I've been having trouble getting a button to run during startup of the
application.  The current code is simply:


connectButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { queue.connect(); } });

What would be the best way to add an OnLoad (?) to simulate clicking the
buton when launching the application.

Refactor your code to cleanly separate any model-layer logic from view- layer or controller-layer code.


The anonymous ActionListener subclass you have above is controller- layer code; its actionPerformed() method should just invoke the equivalent "behavioral" method at the model layer. The code above looks like it's doing the right thing in this case. If you have other such methods that have more code in them, trim it out and put it in its right place...

The end result of a well-factored MVC design is that where you currently want to force "a button to run during startup of the application" you can *just* use the model-layer code, queue.connect(). Any view-layer objects (interface elements) that care about the queue's connection state etc. should be updated automatically, because their updates should be dependent on changes posted by model-layer state, not by direct fiddling from the model- layer code.

At least, that's how I'd do this kind of thing in Cocoa, which makes proper MVC design very easy. (Especially with the combination of Interface Builder, bindings, and key-value observing to handle propagating state automatically between layers.) It doesn't seem like it should be too tough to do in Swing though; you'd just tend to do it by writing glue code like the above anonymous ActionListener subclass.

  -- Chris

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden
References: 
 >Pressing a button during main (From: Dean Shavit <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.