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: ProgressMonitor isn't much of a monitor



Hi Nicholas,


Nicholas R. Rinard wrote:
> [...]
> what i want is a PM that will pop up right away and will stay open
> until the end.

Unfortunately, ProgressMonitor is specifically designed to not popup
immediately.

If you want something that appears immediately, use a JProgressBar.

Me, I use my own implementation of a ProgressMonitor, which shows
immediately, and which combines all monitors into a single JFrame. This
looks very much like the progress monitors used by the Finder.


> for some reason, these two calls do not work to that
> end:
> 
>                    pm.setMillisToDecideToPopup( 1 );
>                    pm.setMillisToPopup( 2 );

This may not work under certain circumstances. It is better if you set these
values to zero.


> furthermore, setting the progress a couple times before entering the
> initial, slow part of the routine doesn't work either:
> 
>                    pm.setProgress( 1 );
>                    pm.setProgress( 10 );
>                    pm.setNote( "Getting all items" );
> 
> i must be missing something obvious here, right? how do i get a PM to
> open immediately or almost immediately?

The ProgressMonitor changes its state only, if the progress interval has
changed by at least 1 percent. This is to save CPU resources.

Unless your progress interval is smaller than 1000, the statements above
will not trigger display of the progress dialog.


Here's how to do it:

1. Use an interval which is less than one hundred and increase it by 1. (1
is used to trigger the immediate popup. If you want to use a larger
interval, increase it by a value which is 1 percent or slightly more).

2. Set MillisToDecideToPoupup and MillisToPopup to zero.

3. Call setProgress with value 1 (or by the 1 percent value if you use a
larger interval).

4. Do your task, and always add 1 to setProgress (or add the 1 percent value
if you use a larger interval).


Below is a sample program.
The sample program counts from 1 to 10. The progress monitor shows up
immediately, so that you can see the '1' being counted. The monitor
disappears immediately once 10 is reached, thus you can see it only count
until '9'.


-Werner



import javax.swing.*;
/**
 * TestProgressMonitor.
 *
 * @author  Werner Randelshofer
 * @version 1.0  22 December 2004  Created.
 */
public class TestProgressMonitor {
    
    /** Creates a new instance. */
    public TestProgressMonitor() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        ProgressMonitor pm = new ProgressMonitor(null, "Counting from 1 to
10", "...", 1, 10+1);
        pm.setMillisToDecideToPopup(0);
        pm.setMillisToPopup(0);
        pm.setProgress(1);

        try {
            for (int i=1; i <= 10 || pm.isCanceled(); i++) {
                pm.setProgress(i+1);
                pm.setNote(""+i);
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
        }
        pm.close();
        System.exit(0);
    }    
}


 _______________________________________________
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: 
 >ProgressMonitor isn't much of a monitor (From: "Nicholas R. Rinard" <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.