zevohuhob
Tech Community Builder
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
Some of the applications perform actions that can take a big amount of time. In this case you need to keep a connection with the user to show that your application doesn't stop to work or a failure happens. For this task a good way is to use a progress bar in your application.
Java Swing library provides a class that implements progress bar:
To test JProgressBar class create new project with class ProgressBarDemo:
All the GUI elements will be defined in the constructor of the class. We will create a frame with 2 buttons and 2 types of progress bars.
To use JProgressBar class you need to add import of this class:
To place all the GUI elements we will use a JPanel with BoxLayout:
A vertical glue is added to the top of the frame for better look of the application.
The first type of JProgressBar is an infinite
progress bar. To create an infinite progress bar you can use the following code:
Now we can add infinite
progress bar to the panel:
Another type of progress bar is a progress bar with percentage:
To show the progress of the action you need set PaintedString:
For this progress bar we can set the range of values. We will use percentage, so the interval is set from 0 to 100:
Now we can add it to the panel:
To test progress bars work we will have a panel with buttons for different actions with progress bars:
Now we can add the buttons to this panel:
The last step with GUI is to add buttons panel to the ProgressPanel:
And the ProgressPanel is added to the frame:
We need to manipulate progress bars. For this purpose we need to add actions listeners for both buttons. The first button Add progress
will increment the progress with 5 percents:
This Listener increment the value of the progress bar if it's possible.
Now we can write the listener for infinite progress bar that will stop the progress:
Now you know the basic operation with progress bars and you can keep the dialog with the user even if your application is performing a long term actions.
Download
Java Swing library provides a class that implements progress bar:
- public
class
JProgressBar
- extends
JComponent
- implements
SwingConstants
, Accessible
To test JProgressBar class create new project with class ProgressBarDemo:
- import
javax.swing.JFrame
;
- public
class
ProgressBarDemo extends
JFrame
{
- ProgressBarDemo(
)
{
- //size,title, exit
- setSize(
400
,400
)
;
- setTitle(
"Progress Bar"
)
;
- setDefaultCloseOperation(
EXIT_ON_CLOSE)
;
- }
- public
static
void
main(
String
[
]
args)
{
- }
- }
All the GUI elements will be defined in the constructor of the class. We will create a frame with 2 buttons and 2 types of progress bars.
To use JProgressBar class you need to add import of this class:
- import
javax.swing.JProgressBar
;
To place all the GUI elements we will use a JPanel with BoxLayout:
- JPanel
ProgressPanel =
new
JPanel
(
)
;
- ProgressPanel.setLayout
(
new
BoxLayout
(
ProgressPanel, BoxLayout
.Y_AXIS
)
)
;
- ProgressPanel.add
(
Box
.createVerticalGlue
(
)
)
;
A vertical glue is added to the top of the frame for better look of the application.
The first type of JProgressBar is an infinite
progress bar. To create an infinite progress bar you can use the following code:
- final
JProgressBar
infinite =
new
JProgressBar
(
)
;
- infinite.setIndeterminate
(
true
)
;
Now we can add infinite
progress bar to the panel:
- ProgressPanel.add
(
infinite)
;
- ProgressPanel.add
(
Box
.createVerticalGlue
(
)
)
;
Another type of progress bar is a progress bar with percentage:
- final
JProgressBar
percentage =
new
JProgressBar
(
)
;
To show the progress of the action you need set PaintedString:
- percentage.setStringPainted
(
true
)
;
For this progress bar we can set the range of values. We will use percentage, so the interval is set from 0 to 100:
- percentage.setMinimum
(
0
)
;
- percentage.setMaximum
(
100
)
;
Now we can add it to the panel:
- ProgressPanel.add
(
percentage)
;
- ProgressPanel.add
(
Box
.createVerticalGlue
(
)
)
;
To test progress bars work we will have a panel with buttons for different actions with progress bars:
- JPanel
buttons =
new
JPanel
(
)
;
- buttons.setLayout
(
new
BoxLayout
(
buttons, BoxLayout
.X_AXIS
)
)
;
- buttons.add
(
Box
.createHorizontalGlue
(
)
)
;
Now we can add the buttons to this panel:
- JButton
add =
new
JButton
(
"Add progress"
)
;
- JButton
infiniteBtn =
new
JButton
(
"Stop"
)
;
- buttons.add
(
add)
;
- buttons.add
(
Box
.createHorizontalGlue
(
)
)
;
- buttons.add
(
infiniteBtn)
;
- buttons.add
(
Box
.createHorizontalGlue
(
)
)
;
The last step with GUI is to add buttons panel to the ProgressPanel:
- ProgressPanel.add
(
buttons)
;
And the ProgressPanel is added to the frame:
- this
.getContentPane
(
)
.add
(
ProgressPanel)
;
We need to manipulate progress bars. For this purpose we need to add actions listeners for both buttons. The first button Add progress
will increment the progress with 5 percents:
- add.addActionListener
(
new
ActionListener
(
)
{
- @Override
- public
void
actionPerformed(
ActionEvent
e)
{
- int
percents =
percentage.getValue
(
)
;
- percents +=
5
;
- if
(
percents <
percentage.getMaximum
(
)
)
- percentage.setValue
(
percents)
;
- else
- percentage.setValue
(
percentage.getMaximum
(
)
)
;
- }
- }
)
;
This Listener increment the value of the progress bar if it's possible.
Now we can write the listener for infinite progress bar that will stop the progress:
- infiniteBtn.addActionListener
(
new
ActionListener
(
)
{
- @Override
- public
void
actionPerformed(
ActionEvent
e)
{
- infinite.setIndeterminate
(
false
)
;
- infinite.setString
(
null
)
;
- }
- }
)
;
Now you know the basic operation with progress bars and you can keep the dialog with the user even if your application is performing a long term actions.
Download
You must upgrade your account or reply in the thread to view hidden text.