Progress Bar in Java Swing library

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:
  1. public class JProgressBar
  2. extends JComponent
To test JProgressBar class create new project with class ProgressBarDemo:
  1. import javax.swing.JFrame;
  2.  
  3. public class ProgressBarDemo extends JFrame{
  4. ProgressBarDemo(){
  5. //size,title, exit
  6. setSize(400,400);
  7. setTitle("Progress Bar");
  8. setDefaultCloseOperation(EXIT_ON_CLOSE);
  9. }
  10. public static void main(String[] args) {
  11.  
  12. }
  13. }
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:
  1. import javax.swing.JProgressBar;
To place all the GUI elements we will use a JPanel with BoxLayout:
  1. JPanel ProgressPanel = new JPanel();
  2. ProgressPanel.setLayout(new BoxLayout(ProgressPanel, BoxLayout.Y_AXIS));
  3. 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:
  1. final JProgressBar infinite = new JProgressBar();
  2. infinite.setIndeterminate(true);
Now we can add
infinite
progress bar to the panel:
  1. ProgressPanel.add(infinite);
  2. ProgressPanel.add(Box.createVerticalGlue());
Another type of progress bar is a progress bar with percentage:
  1. final JProgressBar percentage = new JProgressBar();
To show the progress of the action you need set PaintedString:
  1. 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:
  1. percentage.setMinimum(0);
  2. percentage.setMaximum(100);
Now we can add it to the panel:
  1. ProgressPanel.add(percentage);
  2. ProgressPanel.add(Box.createVerticalGlue());
To test progress bars work we will have a panel with buttons for different actions with progress bars:
  1. JPanel buttons = new JPanel();
  2. buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
  3. buttons.add(Box.createHorizontalGlue());
Now we can add the buttons to this panel:
  1. JButton add = new JButton("Add progress");
  2. JButton infiniteBtn = new JButton("Stop");
  3. buttons.add(add);
  4. buttons.add(Box.createHorizontalGlue());
  5. buttons.add(infiniteBtn);
  6. buttons.add(Box.createHorizontalGlue());
The last step with GUI is to add buttons panel to the ProgressPanel:
  1. ProgressPanel.add(buttons);
And the ProgressPanel is added to the frame:
  1. 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:
  1. add.addActionListener(new ActionListener(){
  2.  
  3. @Override
  4. public void actionPerformed(ActionEvent e) {
  5. int percents = percentage.getValue();
  6. percents += 5;
  7. if(percents < percentage.getMaximum())
  8. percentage.setValue(percents);
  9. else
  10. percentage.setValue(percentage.getMaximum());
  11. }
  12.  
  13. });
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:
  1. infiniteBtn.addActionListener(new ActionListener(){
  2.  
  3. @Override
  4. public void actionPerformed(ActionEvent e) {
  5. infinite.setIndeterminate(false);
  6. infinite.setString(null);
  7.  
  8. }
  9.  
  10. });
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.

Add new comment