Professional Documents
Culture Documents
html
!ara lain seberapa sebuah tugas telah dikerjakan,anda dapat menggunakan progress bar seperti berikut:
ProgressMonitor
" visible component to graphically display how much of a total task has completed. ee #sing $eterminate Progress %ars for information and an e&le of using a typical progress bar. 'he section #sing (ndeterminate )ode tells you how to animate a progress bar to show activity before the task*s scope is known. Not a visible component. (nstead, an instance of this class monitors the progress of a task and pops up a dialog if necessary. ee +ow to #se Progress )onitors for details and an e&le of using a progress monitor.
ProgressMonitorInputStream
"n input stream with an attached progress monitor, which monitors reading from the stream. ,ou use an instance of this stream like any of the other input streams described in %asic (/-. ,ou can get the stream*s progress monitor with a call to getProgressMonitor and configure it as described in +ow to #se Progress )onitors. "fter you see a progress bar and a progress monitor in action, $eciding .hether to #se a Progress %ar or a Progress )onitor can help you figure out which is appropriate for your application.
+ere*s a picture of a small demo application that uses a progress bar to measure the progress of a task that runs in its own thread:
Try this: !lick the /aunch button to run the Progress%ar $emo using 0ava1 .eb tart 2download 0$K 3 or later4. "lternatively, to compile and run the e&le yourself, consult the e&le inde&.
%elow is the code from Progress ar!emo."a#a that creates and sets up the progress bar:
//Where member variables are declared: $Progress ar%progress ar; ... //Where the GUI is constructed: progress ar%&%ne'%$Progress ar(()%tas*.get+engt,OfTas*()); progress ar.set-a.ue((); progress ar.setStringPainted(true); The constructor that creates the progress bar sets the progress bar's minimum and maximum values. You can also set these values with setMinimum and setMa/imum. The minimum and maximum values used in this program are 0 and the length of the task, which is typical of many programs and tasks. However, a progress bar's minimum and maximum values can be any value, even negative. The code snippet also sets the progress bar's current value to 0. The call to setStringPainted causes the progress bar to display, within its bounds, a textual indication of the percentage of the task that has completed. y default, the progress bar displays the value returned by its getPercentComp.ete% method formatted as a percent, such as 33%. !lternatively, you can replace the default with a different string by calling setString. "or example, if%(/*...half way done...*/) %%%%progress ar.setString(01a.f%'a2%t,ere30); #hen the user clicks Start, an instance of the inner class Tas* is created and executed.
pu4.ic%#oid%actionPerformed(Action5#ent%e#t)%6 %%%%start utton.set5na4.ed(fa.se); %%%%setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); %%%%done%&%fa.se; %%%%task = new Task(); task.addPropertyChange istener(this); task.e!e"ute(); 7
is a subclass of "a#a/.s'ing.S'ingWor*er. 'he Tas* instance does three important things for Progress ar!emo:
Tas*
5. 'he instance invokes the doIn ac*ground in a separate thread. 'his is where the long-running task is actually e&ecuted. #sing a background thread instead of the event-dispatching thread prevents the user interface from free6ing while the task is running. 7. .hen the background task is complete, the instance invokes the done method in the event-dispatching thread. 8. 'he instance maintains a bound property, progress, that is updated to indicate the progress of the task. 'he propert2C,ange method is invoked each time progress changes. ee .orker 'hreads and wing.orker in !oncurrency in wing for more information about S'ingWor*er. 'he background task in Progress ar!emo simulates a real task by reporting random amounts of progress at random intervals. 'he propert2C,ange method responds to changes in the the task*s progress property by updating the progress bar:
pu4.ic%#oid%propert2C,ange(Propert2C,ange5#ent%e#t)%6 %%%%if%(3done)%6 %%%%%%%%int%progress%&%tas*.getProgress(); %%%%%%%%progressBar.set#a$ue(progress); %%%%%%%%tas*Output.append(String.format( %%%%%%%%%%%%%%%%0Comp.eted%8d88%of%tas*.9n0)%progress)); %%%%7
.hen the background task is complete, the task*s done method resets the progress bar:
pu4.ic%#oid%done()%6 %%%%::Te..%progress%.istener%to%stop%updating%progress%4ar. %%%%done%&%true; %%%%Too.*it.get!efau.tToo.*it().4eep(); %%%%start utton.set5na4.ed(true); %%%%setCursor(nu..);%::turn%off%t,e%'ait%cursor %%%%progressBar.set#a$ue(progressBar.getMinimum()); %%%%tas*Output.append(0!one39n0); 7
9ote that the done method sets the done field to true, preventing propert2C,ange from making further updates to the progress bar. 'his is necessary because the final updates to the progress property may occcur after done is invoked.
'he other changes in the code are related to string display. " progress bar that displays a string is likely to be taller than one that doesn*t, and, as the demo designers, we*ve arbitarily decided that this progress bar should display a string only when it*s in the default, determinate mode. +owever, we want to avoid the layout ugliness that might result if the progress bar changed height when it changed modes. 'hus, the code leaves in the call to setStringPainted(true) but adds a call to setString(00) so that no te&t will be displayed. /ater, when the progress bar switches from indeterminate to determinate mode, invoking setString(nu..) makes the progress bar display its default string. -ne change we did not make was removing the call to progress ar.set-a.ue from the progress event handler. 'he call doesn*t do any harm because an indeterminate progress bar doesn*t use its value property, e&cept perhaps to display it in the status string. (n fact, keeping the progress bar*s data as up-to-date as possible is a good practice, since some look and feels might not support indeterminate mode. Try this: 5. !lick the /aunch button to run the Progress%ar7 $emo using 0ava1 .eb tart 2download 0$K 3 or later4. "lternatively, to compile and run the e&le yourself, consult the e&le inde&. 7. Push the Start button. 9ote that the progress bar starts animating as soon as the button is pressed, and then switches back into determinate mode 2like Progress%ar$emo4.
Try this: 5. !lick the /aunch button to run the Progress)onitor $emo using 0ava1 .eb tart 2download 0$K 3 or later4. "lternatively, to compile and run the e&le yourself, consult the e&le inde&. 7. Push the Start button. "fter a certain amount of time, the program displays a progress dialog. 8. !lick the OK button. 9ote that the task continues even though the dialog is gone. :. tart another task. "fter the dialog pops up, click the Cancel button. 'he dialog goes away and the task stops.
" progress monitor cannot be used again, so a new one must be created each time a new task is started. 'his program creates a progress monitor each time the user starts a new task with the Start button. +ere*s the statement that creates the progress monitor:
progressMonitor%&%ne'%ProgressMonitor(ProgressMonitor!emo.t,is) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%0Running%a%+ong%Tas*0) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%00)%()% tas*.get+engt,OfTas*());
'his code uses ProgressMonitor*s only constructor to create the monitor and initiali6e several arguments:
'he first argument provides the parent component to the dialog popped up by the progress monitor.
'he second argument is a string that describes the nature of the task being monitored. 'his string is displayed on the dialog. see 'he Progress )onitoring "P( for details about this argument. 'he third argument is another string that provides a changeable status note. 'he e&le uses an empty string to indicate that the dialog should make space for a changeable status note, but that the note is initially empty. (f you provide nu.. for this argument, the note is omitted from the dialog. 'he e&le updates the note each time the progress property changes. (t updates the monitor*s current value at the same time:
int%progress%&%tas*.getProgress(); String%message%&%String.format(0Comp.eted%8d88.9n0)%progress); progressMonitor.set&ote(message); progressMonitor.setProgress(progress); tas*Output.append(message);
'he last two arguments provide the minimum and ma&imum values, respectively, for the progress bar displayed in the dialog.
%y default, a progress monitor waits a minium of ;<< milliseconds before deciding whether to pop up the dialog. (t also waits for the progress to become more than the minimum value. (f it calculates that the task will take more than 7<<< milliseconds to complete, the progress dialog appears. 'o adjust the minimum waiting period, invoke setMi..isTo!ecidedToPopup. 'o adjust the minimum progress time re=uired for a dialog to appear, invoke setMi..isToPopup. %y the simple fact that this e&le uses a progress monitor, it adds a feature that wasn*t present in the version of the program that uses a progress bar: 'he user can cancel the task by clicking the Cancel button on the dialog. +ere*s the code in the e&le that checks to see if the user canceled the task or if the task e&ited normally:
if%(progressMonitor.isCance.ed()%==%tas*.is!one())%6 %%%%progressMonitor.c.ose(); %%%%Too.*it.get!efau.tToo.*it().4eep(); %%%%if%(progressMonitor.isCance.ed())%6 %%%%%%%%tas*.cance.(true); %%%%%%%%tas*Output.append(0Tas*%cance.ed.9n0); %%%%7%e.se%6 %%%%%%%%tas*Output.append(0Tas*%comp.eted.9n0); %%%%7 %%%%start utton.set5na4.ed(true); 7
9ote that the progress monitor doesn*t itself cancel the task. (t provides the >#( and "P( to allow the program to do so easily.
,ou want more control over the configuration of the progress bar. (f you are working directly with a progress bar, you can set it to be indeterminate, make it display vertically, provide a string for it to display, register change listeners on it, and provide it with a bounded range model to control the progress bar*s minimum, ma&imum, and current values. 'he program needs to display other components along with the progress bar. ,ou need more than one progress bar. .ith some tasks, you need to monitor more than one parameter. ?or e&le, an installation program might monitor disk space usage in addition to how many files have been successfully installed. ,ou need to reuse the progress bar. " progress bar can be reused@ a progress monitor cannot. -nce the progress monitor has decided to display a dialog 2or not4, the progress monitor cannot do it again.
,ou want an easy way to display progress in a dialog. 'he running task is secondary and the user might not be interested in the progress of the task. Progress monitor provides a way for the user to dismiss the dialog while the task is still running. ,ou want an easy way for the task to be cancelled. Progress monitor provides a >#( for the user to cancel the task. "ll you have to do is call progress monitor*s isCance.ed method to find out if the user pressed the Cancel button. ,our task displays a short message periodically while running. 'he progress monitor dialog provides the set<ote method so that the task can provide further information about what it*s doing. ?or e&le, an installation task might report the name of each file as it*s installed. 'he task might not take a long time to complete. ,ou decide at what point a running task is taking long enough to warrant letting the user know about it. Progress monitor won*t pop up a dialog if the task completes within the timeframe you set.
(f you decide to use a progress monitor and the task you are monitoring is reading from an input stream, use the ProgressMonitorInputStream class.
etting or >etting the Progress %ar*s !onstraints/Aalues !ontrolling the Progress %ar*s "ppearance !reating the Progress )onitor !onfiguring the Progress )onitor 'erminating the Progress )onitor !reating the Progress %ar Constr!ctor P!r"ose !reate a hori6ontal progress bar. 'he noargument constructor initiali6es the progress bar with a minimum and initial value of < and a ma&imum of 5<<. 'he constructor with two integer arguments specifies the minimum and ma&imum values. !reate a progress bar with the specified orientation, which can be either $Progress ar.1ORI>O<TA+ or $Progress ar.-5RTICA+. 'he optional second and third arguments specify minimum and ma&imum values. !reate a hori6ontal progress bar with the specified range model. P!r"ose et or get the current value of the progress bar. 'he value is constrained by the minimum and ma&imum values. >et the percent complete for the progress bar. et or get the minimum value of the progress bar. et or get the ma&imum value of the progress bar.
0Progress%ar2%oundedBange)odel4
etting or >etting the Progress %ar*s !onstraints/Aalues Method void setAalue2int4 int getAalue24 double getPercent!omplete24 void set)inimum2int4 int get)inimum24 void set)a&imum2int4 int get)a&imum24
et or get the model used by the progress bar. 'he void model establishes the progress bar*s constraints set)odel2%oundedBange)odel4 and values, so you can use it directly as an %oundedBange)odel get)odel24 alternative to using the individual set/get methods listed above. !ontrolling the Progress %ar*s "ppearance Method P!r"ose %y specifying true, put the progress bar into void indeterminate mode. pecifying fa.se puts the progress set(ndeterminate2boolean4 bar back into its default, determinate mode.
void set-rientation2int4 int get-rientation24 void set%orderPainted2boolean4 boolean is%orderPainted24 void set tringPainted2boolean4 boolean is tringPainted24 void set tring2 tring4 tring get tring24
et or get whether the progress bar is vertical or hori6ontal. "cceptable values are $Progress ar.-5RTICA+ or $Progress ar.1ORI>O<TA+. et or get whether the progress bar has a border. et or get whether the progress bar displays a percent string. %y default, the value of the percent string is the value returned by getPercentComp.ete formatted as a percent. ,ou can set the string to be displayed with setString. et or get the percent string. !reating the Progress )onitor
Method or Constr!ctor
P!r"ose !reate a progress monitor. 'he Component argument is the parent for the monitor*s dialog. 'he O4"ect argument is a message to put on the option pane within the dialog. 'he value of this object is typically a String. 'he String argument is a changeable status note. 'he final two int arguments set the minimum and ma&imum values, respectively, for the progress bar used in the dialog. >ets a progress monitor that monitors reading from an input stream.
!onfiguring the Progress )onitor Method void set)inimum2int4 int get)inimum24 void set)a&imum2int4 int get)a&imum24 void setProgress2int4
#oid%set<ote(String) String%get<ote()
P!r"ose et or get the minimum value of the progress monitor. 'his value is used by the monitor to set up the progress bar in the dialog. et or get the ma&imum value of the progress monitor. 'his value is used by the monitor to set up the progress bar in the dialog. #pdate the monitor*s progress. et or get the status note. 'his note is displayed on the dialog. 'o omit the status note from the dialog, provide nu.. as the third argument to the monitor*s constructor.
et or get the time after which the monitor should decide whether to popup a dialog. P!r"ose
!lose the progress monitor. 'his disposes of the dialog. $etermine whether the user pressed the Cancel button.
&otes #ses a basic progress bar to show progress on a task running in a separate thread. #ses a basic progress bar to show progress on a task running in a separate thread. )odification of the previous e&le that uses a progress monitor instead of a progress bar.
(f you are programming in 0ava?C, see Progress %ar and Progress (ndicator.