How to Use the progress bar progressmonitor jprogressbar.

Source: Internet
Author: User
How to Use the progress bar progressmonitor jprogressbar.

 

Note: The jprogressbar is added to the Panel, and the progressmonitor is a new dialog box.

The uncertainty of jprogressbar refers to the uncertainty of the remaining time, and the progress is expressed by percentage. The default value is. Setting Method: jprogressbar. setstringpainted (true); if the default value is false, you must set the display content.

------------------------ English original: http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html Chinese Translation: Java uncertain progress bar ---------------------------------------------------------------------------- content: the basic usage of the progress bar
Use uncertain Mode
How does Sun use progress bars? Java Theory and Practice: Hi, where is my thread?
Scheduled repeated tasks in Java applications
IBM developer kits for Java (downloads)
Developerworks toolbox subscribes to a subtle but important update of jprogressbar
Level: getting started with John zukoski (jaz@zukowski.net

)
President, JZ ventures, Inc.
In December 2003, developers used the jprogressbar component to display the progress of a task to users. For long tasks or tasks that are difficult to accurately determine the completion progress, Merlin
The version adds an uncertain mode to the jprogressbar. This month, columnist John zukoski's jprogressbar
And discusses its new uncertain mode. You can go to the forum in this article
Share your thoughts with the author and other readers (you can also click the discussion at the top or bottom of the article to enter the Forum ).
In Java 2 SDK, standard
Edition, version 1.4, there are some major changes, such as the addition of new components such as jspinner, springlayout
Such a new layout manager or a new API like Java logging API. Other changes are not so significant, such as small improvements or
. This month's springlayout component skills are in the latter case. It is subtle, but important. Basic usage of progress bars
Jprogressbar is the original swing
A component set. It provides a graphical display of the progress of the process. When a process is in progress, a long bar will gradually extend on the component until the task is completed and the bars are full. Long
Bar Movement is usually part of a multi-threaded task, which helps avoid blocking the rest of the application progress, such as regular screen updates. Although no specific rule says that the progress bar must be linearly shifted
Dynamic, but if you see the process moving from 10% to 35%, then return to 27%, and then add to 80%, and finally end with 0%, as a user, I will feel a little strange. Use one of the five constructors to construct the jprogressbar, as shown in Listing 1. jprogressbar constructor public jprogressbar ()
Public jprogressbar (INT orientation)
Public jprogressbar (INT minimum, int maximum)
Public jprogressbar (INT orientation, int minimum, int maximum)
Public jprogressbar (boundedrangemodel model) jprogressbar needs to initialize the direction and value range. The direction is determined by the vertical or horizontal constant of the jprogressbar. The default value is horizontal. After this component is created and displayed on the screen, start the second thread to execute the task to measure its progress. Then, the progress bar value is updated regularly using the setvalue () method to display the current progress of the task. Listing 2 shows a simple example of setvalue (): Listing 2. Simple jprogressbar usage
Import javax. Swing .*;
Import java. AWT .*;
Import java. AWT. event .*;
Import java. Lang. Reflect. *; public class progresssample {
Static class extends read extends thread {
Private Static int delay= 500;
Jprogressbar progressbar; Public barthread (jprogressbar bar ){
Progressbar = bar;
} Public void run (){
Int minimum = progressbar. getminimum ();
Int maximum = progressbar. getmaximum ();
Runnable runner = new runnable (){
Public void run (){
Int value = progressbar. getvalue ();
Progressbar. setvalue (Value + 1 );
}
};
For (INT I = minimum; I <maximum; I ++ ){
Try {
Swingutilities. invokeandwait (runner );
// Our task for each step is to just sleep
Thread. Sleep (Delay );
} Catch (interruptedexception ignoredexception ){
} Catch (invocationtargetexception ignoredexception ){
}
}
}
}
Public static void main (string ARGs []) {
// Initialize
Final jprogressbar ajprogressbar = new jprogressbar (0,100); Final jbutton ajbutton = new jbutton ("START"); actionlistener = new actionlistener (){
Public void actionreceivmed (actionevent e ){
Ajbutton. setenabled (false );
Thread stepper = new barthread (ajprogressbar );
Stepper. Start ();
}
}; Ajbutton. addactionlistener (actionlistener); jframe theframe = new jframe ("progress bars ");
Theframe. setdefaclocloseoperation (jframe. exit_on_close );
Container contentpane = theframe. getcontentpane ();
Contentpane. Add (ajprogressbar, borderlayout. North );
Contentpane. Add (ajbutton, borderlayout. South );
Theframe. setsize (300,100 );
Theframe. Show ();
}
} When you run this code for the first time, you will see a screen similar to figure 1. Figure 1. Start Screen
Click this button to start the second task and update the progress bar when it is running. Figure 2 shows the progress bar halfway through. Figure 2. display progress on the screen
There is nothing special here. The main Code creates a GUI with a button and a progress bar. When you select the button, it triggers an operation to update the progress bar. A progress bar is used to measure tasks. In the example program, this task is sleep for 100 times in half a second. By default, there is no graphical indication of the Progress except the progress bar. Add the following line of code to show the progress bar the percentage of completed tasks during task completion: ajprogressbar. setstringpainted (true); Figure 3 shows the screen for adding new code:
 
Figure 3. display the uncertain mode of completion percentage
Since the Merlin version, jprogressbar also supports another mode ??
OK. This mode can be used for long tasks with non-fixed steps. It displays a fixed animation to indicate that something is happening, but it does not indicate the percentage of completion. If you have determined the time required for the task
You can switch back to the confirmation mode. In uncertain mode, the jprogressbar displays a long bar that moves back and forth in the display area. Listing 3 shows an example of this mode. The new method is setindeterminate (). If the value is true, it means uncertainty, and if the value is false, it means normal or OK. Listing 3. uncertain progress bar import javax. Swing .*;
Import java. AWT .*;
Import java. AWT. event. *; public class progresssample2 {
Public static void main (string ARGs []) {
Final jprogressbar ajprogressbar = new jprogressbar (0,100 );
Ajprogressbar. setindeterminate (true); jbutton ajbutton = new jbutton ("toggle"); actionlistener = new actionlistener (){
Public void actionreceivmed (actionevent e ){
Boolean indeterminate = ajprogressbar. isindeterminate ();
Ajprogressbar. setindeterminate (! Indeterminate );
}
}; Ajbutton. addactionlistener (actionlistener); jframe theframe = new jframe ("Indeterminate ");
Theframe. setdefaclocloseoperation (jframe. exit_on_close );
Container contentpane = theframe. getcontentpane ();
Contentpane. Add (ajprogressbar, borderlayout. North );
Contentpane. Add (ajbutton, borderlayout. South );
Theframe. setsize (300,100 );
Theframe. Show ();
}
} Figure 4 shows the related screen (you need to imagine that the bar is moving back and forth ). The button is used to switch between the uncertain progress bar and the definite mode.
 
Figure 4. The uncertain mode has two new UI default values used to change the repainting interval and loop time: progressbar. repaintinterval and
Progressbar. cycletime. Change these settings ?? As shown below ??
The display speed is changed. The cycle time must be an even multiple of the re-painting interval. Therefore, if the interval is 100, the cycle time should be 200, 500, or 1000 ?? But not
750. Uimanager. Put ("progressbar. repaintinterval", new INTEGER (150 ));
Uimanager. Put ("progressbar. cycletime", new INTEGER (1050); note that you need to set these values before creating the progress bar. Conclusion
Progressbar also has a lot of content, but other features are in 1.4
Version has not changed. We briefly reviewed the original usage of this component and introduced its new features in the Merlin version. You can also consider using
Progressmonitor or progressmonitorinputstream monitors the progress of these long tasks. They are all used internally.
Advantages of jprogressbar. Reference to participate in the Forum related to this article (you can also click the Forum at the top or bottom of the article to visit the Forum ). Read through John zukoski's complete set of magic techniques for Merlin. Read the javadocs of the jprogressbar, progressmonitor, and progressmonitorinputstream classes. Kelvin Lawrence's "the Java foundation classes: the new
Standard for Java GUI development "(developerworks, May 1998), this article describes
The jfc class is part of the swing UI framework. Read Tom White's "scheduled repeated tasks in a Java application" (developerworks, February November 2003) to learn more about arranging these uncertain jprogressbar update tasks. From Brian Goetz's "Java Theory and Practice: Hi, where is my thread going ?" (Developerworks, September 2002) learn more about management threads. Learn about Sun's Java tutorial lesson on jprogressbar. About the author
John zukoski works as a strategic Java consultant for JZ ventures, inc., through
Answersquad.com provides technical support and uses savaje technology to develop the next-generation mobile phone platform. His recent works include mastering Java
2, j2se 1.4 (sybex, March April 2002) and learn Java with JBuilder 6
(Apress, December March 2002 ). Available via jaz@zukowski.net

Contact John. Java uncertain progress bar ---------------------------------------------------------------------------- content: basic usage of the progress bar
Use uncertain Mode
Conclusion
References
About the author
Related content of this article: Java Theory and Practice: Hi, where is my thread?
Scheduled repeated tasks in Java applications
IBM developer kits for Java (downloads)
Developerworks toolbox subscribes to a subtle but important update of jprogressbar
Level: getting started with John zukoski (jaz@zukowski.net

)
President, JZ ventures, Inc.
In December 2003, developers used the jprogressbar component to display the progress of a task to users. For long tasks or tasks that are difficult to accurately determine the completion progress, Merlin
The version adds an uncertain mode to the jprogressbar. This month, columnist John zukoski's jprogressbar
And discusses its new uncertain mode. You can go to the forum in this article
Share your thoughts with the author and other readers (you can also click the discussion at the top or bottom of the article to enter the Forum ).
In Java 2 SDK, standard
Edition, version 1.4, there are some major changes, such as the addition of new components such as jspinner, springlayout
Such a new layout manager or a new API like Java logging API. Other changes are not so significant, such as small improvements or
. This month's springlayout component skills are in the latter case. It is subtle, but important. Basic usage of progress bars
Jprogressbar is the original swing
A component set. It provides a graphical display of the progress of the process. When a process is in progress, a long bar will gradually extend on the component until the task is completed and the bars are full. Long
Bar Movement is usually part of a multi-threaded task, which helps avoid blocking the rest of the application progress, such as regular screen updates. Although no specific rule says that the progress bar must be linearly shifted
Dynamic, but if you see the process moving from 10% to 35%, then return to 27%, and then add to 80%, and finally end with 0%, as a user, I will feel a little strange. Use one of the five constructors to construct the jprogressbar, as shown in Listing 1. jprogressbar constructor public jprogressbar ()
Public jprogressbar (INT orientation)
Public jprogressbar (INT minimum, int maximum)
Public jprogressbar (INT orientation, int minimum, int maximum)
Public jprogressbar (boundedrangemodel model) jprogressbar needs to initialize the direction and value range. The direction is determined by the vertical or horizontal constant of the jprogressbar. The default value is horizontal. After this component is created and displayed on the screen, start the second thread to execute the task to measure its progress. Then, the progress bar value is updated regularly using the setvalue () method to display the current progress of the task. Listing 2 shows a simple example of setvalue (): Listing 2. Simple jprogressbar usage
Import javax. Swing .*;
Import java. AWT .*;
Import java. AWT. event .*;
Import java. Lang. Reflect. *; public class progresssample {
Static class extends read extends thread {
Private Static int delay= 500;
Jprogressbar progressbar; Public barthread (jprogressbar bar ){
Progressbar = bar;
} Public void run (){
Int minimum = progressbar. getminimum ();
Int maximum = progressbar. getmaximum ();
Runnable runner = new runnable (){
Public void run (){
Int value = progressbar. getvalue ();
Progressbar. setvalue (Value + 1 );
}
};
For (INT I = minimum; I <maximum; I ++ ){
Try {
Swingutilities. invokeandwait (runner );
// Our task for each step is to just sleep
Thread. Sleep (Delay );
} Catch (interruptedexception ignoredexception ){
} Catch (invocationtargetexception ignoredexception ){
}
}
}
}
Public static void main (string ARGs []) {
// Initialize
Final jprogressbar ajprogressbar = new jprogressbar (0,100); Final jbutton ajbutton = new jbutton ("START"); actionlistener = new actionlistener (){
Public void actionreceivmed (actionevent e ){
Ajbutton. setenabled (false );
Thread stepper = new barthread (ajprogressbar );
Stepper. Start ();
}
}; Ajbutton. addactionlistener (actionlistener); jframe theframe = new jframe ("progress bars ");
Theframe. setdefaclocloseoperation (jframe. exit_on_close );
Container contentpane = theframe. getcontentpane ();
Contentpane. Add (ajprogressbar, borderlayout. North );
Contentpane. Add (ajbutton, borderlayout. South );
Theframe. setsize (300,100 );
Theframe. Show ();
}
} When you run this code for the first time, you will see a screen similar to figure 1. Figure 1. Start Screen
Click this button to start the second task and update the progress bar when it is running. Figure 2 shows the progress bar halfway through. Figure 2. display progress on the screen
There is nothing special here. The main Code creates a GUI with a button and a progress bar. When you select the button, it triggers an operation to update the progress bar. A progress bar is used to measure tasks. In the example program, this task is sleep for 100 times in half a second. By default, there is no graphical indication of the Progress except the progress bar. Add the following line of code to show the progress bar the percentage of completed tasks during task completion: ajprogressbar. setstringpainted (true); Figure 3 shows the screen for adding new code:
 
Figure 3. display the uncertain mode of completion percentage
Since the Merlin version, jprogressbar also supports another mode ??
OK. This mode can be used for long tasks with non-fixed steps. It displays a fixed animation to indicate that something is happening, but it does not indicate the percentage of completion. If you have determined the time required for the task
You can switch back to the confirmation mode. In uncertain mode, the jprogressbar displays a long bar that moves back and forth in the display area. Listing 3 shows an example of this mode. The new method is setindeterminate (). If the value is true, it means uncertainty, and if the value is false, it means normal or OK. Listing 3. uncertain progress bar import javax. Swing .*;
Import java. AWT .*;
Import java. AWT. event. *; public class progresssample2 {
Public static void main (string ARGs []) {
Final jprogressbar ajprogressbar = new jprogressbar (0,100 );
Ajprogressbar. setindeterminate (true); jbutton ajbutton = new jbutton ("toggle"); actionlistener = new actionlistener (){
Public void actionreceivmed (actionevent e ){
Boolean indeterminate = ajprogressbar. isindeterminate ();
Ajprogressbar. setindeterminate (! Indeterminate );
}
}; Ajbutton. addactionlistener (actionlistener); jframe theframe = new jframe ("Indeterminate ");
Theframe. setdefaclocloseoperation (jframe. exit_on_close );
Container contentpane = theframe. getcontentpane ();
Contentpane. Add (ajprogressbar, borderlayout. North );
Contentpane. Add (ajbutton, borderlayout. South );
Theframe. setsize (300,100 );
Theframe. Show ();
}
} Figure 4 shows the related screen (you need to imagine that the bar is moving back and forth ). The button is used to switch between the uncertain progress bar and the definite mode.
 
Figure 4. The uncertain mode has two new UI default values used to change the repainting interval and loop time: progressbar. repaintinterval and
Progressbar. cycletime. Change these settings ?? As shown below ??
The display speed is changed. The cycle time must be an even multiple of the re-painting interval. Therefore, if the interval is 100, the cycle time should be 200, 500, or 1000 ?? But not
750. Uimanager. Put ("progressbar. repaintinterval", new INTEGER (150 ));
Uimanager. Put ("progressbar. cycletime", new INTEGER (1050); note that you need to set these values before creating the progress bar. Conclusion
Progressbar also has a lot of content, but other features are in 1.4
Version has not changed. We briefly reviewed the original usage of this component and introduced its new features in the Merlin version. You can also consider using
Progressmonitor or progressmonitorinputstream monitors the progress of these long tasks. They are all used internally.
Advantages of jprogressbar. Reference to participate in the Forum related to this article (you can also click the Forum at the top or bottom of the article to visit the Forum ). Read through John zukoski's complete set of magic techniques for Merlin. Read the javadocs of the jprogressbar, progressmonitor, and progressmonitorinputstream classes. Kelvin Lawrence's "the Java foundation classes: the new
Standard for Java GUI development "(developerworks, May 1998), this article describes
The jfc class is part of the swing UI framework. Read Tom White's "scheduled repeated tasks in a Java application" (developerworks, February November 2003) to learn more about arranging these uncertain jprogressbar update tasks. From Brian Goetz's "Java Theory and Practice: Hi, where is my thread going ?" (Developerworks, September 2002) learn more about management threads. Learn about Sun's Java tutorial lesson on jprogressbar. About the author
John zukoski works as a strategic Java consultant for JZ ventures, inc., through
Answersquad.com provides technical support and uses savaje technology to develop the next-generation mobile phone platform. His recent works include mastering Java
2, j2se 1.4 (sybex, March April 2002) and learn Java with JBuilder 6
(Apress, December March 2002 ). Available via jaz@zukowski.net

Contact John.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.