How To Create A Progress Bar In Java
ProgressMonitor ProgressMonitorInputStream Technically speaking, a JProgressBar displays an integer value within a bounded interval. Realistically speaking, you use a
JProgressBar to display the progress of a long-running task. JProgressBar is for display purposes only. To allow the user to set a bounded integer value, use a slider.
Here's a picture of a small demo application that uses a progress bar to measure the progress of a long task running in a separate thread:
Below is the code from
Try this:
- Compile and run the application. The main source file is
ProgressBarDemo.java. You will also needLongTask.javaandSwingWorker.java.
See Getting Started with Swing if you need help.- Push the Start button. Watch the progress bar as the task makes progress.
ProgressBarDemo.java that creates and sets up the progress bar: Once the task has begun, a timer causes the progress bar to update every second until the task completes. The timer is implemented with theprogressBar = new JProgressBar(); progressBar.setMinimum(0); progressBar.setMaximum(task.getLengthOfTask()); progressBar.setValue(0); . . . //add the progress bar to the window's content pane contentPane.add(progressBar);
Timer class, which is described in How to Use Timers. The progress bar measures the progress made by the task each second, not the elapsed time. The following line of code appears in the actionPerformed method of the timer's action listener. Once a second, the timer fires an action event, which causes the program to find the amount of work completed by the task and use that value to update the progress bar.progressBar.setValue(task.getCurrent());
As mentioned, the long-running task in this program runs in a separate thread. The long-running task is implemented by LongTask.java which uses a SwingWorker. See Using the SwingWorker Class in Threads and Swing for information about the SwingWorker class.
The Progress Bar API
The following tables list the commonly usedJProgressBarconstructors and methods. Other methods you're likely to call are defined by theJComponentand
Componentclasses and include [PENDING: anything in particular for JProgressBar?]. [Link to JComponent and Component discussions.]
The API for using progress bars falls into two categories:
- Setting or Getting the Progress Bar's Constraints/Values
- Fine Tuning the Progress Bar's Appearance
Setting or Getting the Progress Bar's Constraints/Values Method Purpose void setValue(int)
int getValue()Set or get the current value of the progress bar. The value is constrained by the minimum and maximum values. void setMinimum(int)
int getMinimum()Set or get the minimum value of the progress bar. void setMaximum(int)
int getMaximum()Set or get the maximum value of the progress bar. void setModel(BoundedRangeModel)
BoundedRangeModel getMaximum()Set or get the model used by the progress bar. The model establishes the progress bar's constraints and values. So you can use this method as an alternative to using the individual set/get methods listed above.
Fine Tuning the Progress Bar's Appearance Method Purpose void setOrientation(int)
int getOrientation()Set or get whether the progress bar is vertical or horizontal. Acceptable values are JProgressBar.VERTICALorJProgressBar.HORIZONTAL.void setBorderPainted(boolean)
boolean isBorderPainted()Set or get whether the progress bar has a border.
Examples that Use JProgressBar
This table shows the examples that useJProgressBarand where those examples are described.
Example Where Described ProgressBarDemo.javaThis page and How to Use Timers
How To Create A Progress Bar In Java
Source: http://www.cs.fsu.edu/~jtbauer/cis3931/tutorial/ui/swing/progressbar.html
Posted by: gordonhatelve.blogspot.com

0 Response to "How To Create A Progress Bar In Java"
Post a Comment