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



Florijan Stamenkovic wrote:

1. If you just want to 'simulate' a button click, there is a handy
method in AbstractButton class doClick(), where the GUI of the button
reflects the "being clicked state". Would not try to call it from any
thread other then the Event Dispatch Thread (if you are not familiar
with it, check out tutorials on Sun's website).
2. If you want to run the same method as the mouse click, just call
the method. If you are trying this and having problems, it might be
that you are encountering some Swing threading issues.

The other issue is knowing when your app (and Swing) are up and running, and ready to process a button click. That can be tricky, especially if you are following Sun's best-practice guidelines and creating all your GUI elements on the Swing thread. This gets tricky because the bulk of your startup code runs on a "normal" thread (to avoid locking up the UI for the duration), placing small tasks onto the Swing thread throughout the startup cycle. At some point, however, your code will have finished queuing up work to be performed on the Swing thread. At that point you can queue your "button" to be run, either by simulating the click to get the visual effects of arming and firing the button presented to the user, or by directly calling the underlying action routine:


    SwingUtilities.invokeLater (new Runnable() {
        public void run() {
            connectButton.doClick();
        }
    });

The doClick call will not be made until all previously queued events have been processed. If any of the pending events queue things to run at a later time on the Swing thread, you may have a problem (e.g. creating a component spawns its own worker thread to perform some time-consuming initialization function, and then place an event on the Swing thread to update the component with the results of the function). In that situation you'll need to defer your button click until all your asynchronous initialization operations complete.
_______________________________________________
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


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.