Swing - SPLessons

Swing ProgressBar

Chapter 20

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Swing ProgressBar

Swing ProgressBar

shape Description

Swing ProgressBar represents the progress of the task in percent format. Before digital marketing progressBar was used in computer environment to know the status of the transferring data from one system to other devices such as pen drive.

shape Syntax

Here class name should extend JComponent to inherit the properties and should implement SwingConstants.

public class JProgressBar extends

JComponent implements

SwingConstants, Accessible


JProgressBar

shape Example


Create the package and import Swing containers to inherit the properties and methods.

package swing;

import java.awt.Container;

Create a class Swing ProgressBar that should extend JFrame, ProgressBar will hire the properties from the JFrame.

public class ProgressBar extends JFrame

Create required events i.e create timer to check the status, create Swing ProgressBar to scroll from starting to ending, Create button such as start or stop .

private Timer timer;
    private JProgressBar pbar;
    private JButton sBtn;

Create a constructor that should be same as class name and create the method inside the constructor.

public ProgressBar() {
        
        initUI();
    }

Call the created method and create object to Swing ProgressBar, add paint to the progress scrolling status.

pbar = new JProgressBar();
        pbar.setStringPainted(true);

Create object to the start button and implement ActionInterface interface. When user click on start button ,action will be performed.

sBtn = new JButton("Start");
        sBtn.addActionListener(new ClickAction());

Add listener to the timer that is UpdateBarListener().

timer = new Timer(50, new UpdateBarListener());

Create layout to the ProgressBar and start button and create title to the window, add termination method to the window.

createLayout(pbar, sBtn);
        
        setTitle("JProgressBar");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

Create the group layout.

Container pane = getContentPane();
        GroupLayout gl = new GroupLayout(pane);
        pane.setLayout(gl);    

Turn on gaps between the components automatically.

gl.setAutoCreateContainerGaps(true);

Create the sequential group at horizontal axis and create parallel group at vertical axis.

gl.setHorizontalGroup(gl.createSequentialGroup()
                .addComponent(arg[0])
                .addComponent(arg[1])
        );

        gl.setVerticalGroup(gl.createParallelGroup(CENTER)
                .addComponent(arg[0])
                .addComponent(arg[1])
        );       

Implement ActionListener on start button  and set the percent to 100. If status crosses the 100 then automatically terminates program.

 private class UpdateBarListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            
                int val = pbar.getValue();
                
                if (val >= 100) {
                    
                    timer.stop();
                    sBtn.setText("End");
                    return;
                }

                pbar.setValue(++val); 

ProgressBar.java

package swing;

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import static javax.swing.GroupLayout.Alignment.CENTER;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.Timer;


public class ProgressBar extends JFrame {

    private Timer timer;
    private JProgressBar pbar;
    private JButton sBtn;

    public ProgressBar() {
        
        initUI();
    }

    private void initUI() {

        pbar = new JProgressBar();
        pbar.setStringPainted(true);
        
        sBtn = new JButton("Start");
        sBtn.addActionListener(new ClickAction());

        timer = new Timer(50, new UpdateBarListener());

        createLayout(pbar, sBtn);
        
        setTitle("JProgressBar");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }
    
    private void createLayout(JComponent... arg) {
        
        Container pane = getContentPane();
        GroupLayout gl = new GroupLayout(pane);
        pane.setLayout(gl);        

        gl.setAutoCreateContainerGaps(true);
        gl.setAutoCreateGaps(true);

        gl.setHorizontalGroup(gl.createSequentialGroup()
                .addComponent(arg[0])
                .addComponent(arg[1])
        );

        gl.setVerticalGroup(gl.createParallelGroup(CENTER)
                .addComponent(arg[0])
                .addComponent(arg[1])
        );       

        pack();
    }    
    
    private class UpdateBarListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            
                int val = pbar.getValue();
                
                if (val >= 100) {
                    
                    timer.stop();
                    sBtn.setText("End");
                    return;
                }

                pbar.setValue(++val);           
        }
    }
    
    private class ClickAction extends AbstractAction {

        @Override
        public void actionPerformed(ActionEvent e) {
            
                if (timer.isRunning()) {
                    
                    timer.stop();
                    sBtn.setText("Start");

                } else if (!"End".equals(sBtn.getText())) {
                    
                    timer.start();
                    sBtn.setText("Stop");
                }       
        }
    }

    public static void main(String[] args) {
        
        EventQueue.invokeLater(new Runnable() {
            
            @Override
            public void run() {
                ProgressBar ex = new ProgressBar();
                ex.setVisible(true);
            }
        });
    }
}

Output:
Output will be as follows, when click on start button data transferring will be started in percent format, after completion of the progress it will be ended.

Summary

shape Key Points

  • To create the gap between the components developer will use setAutoCreateContainerGaps(true);.
  • While fetching the data from one device to another device, data will be visible in percent format.