In this tutorials, I am going to show how to create a most useful component in Swing ProgressBar.

Swing ProgressBar :

Sometimes tasks within the programs might take a long time to complete that’s because of many factors. But to provide the better user-friendly to the end user, it is always good to show the progressbar for how long the task might take, and how much work has already been done.

This can be achieved in Java using Swing ProgressBar, in the below example I am going to read file data with a progress bar.

ProgressBar Example :
package com.swing.examples;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ProgressBarDemo extends JFrame {

    JTextField fileName;
    JButton open,clear;
    JTextArea displayData;
    JProgressBar progressBar;
    JPanel topPanel,buttonsPanel,filePanel;
    File file;
    DataInputStream dataInputStream;
        super("ProgressBar Demo");
        displayData = new JTextArea();
        fileName = new JTextField(30);
        open = new JButton("Open");
        clear = new JButton("Clear");
        progressBar = new JProgressBar();
        topPanel = new JPanel();
        buttonsPanel = new JPanel();
        filePanel = new JPanel();
        topPanel.setLayout(new GridLayout(2,1));
        filePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        buttonsPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        filePanel.add(new JLabel("File Name"));
        getContentPane().add(new JScrollPane(displayData),BorderLayout.CENTER);
        // Adding Listeners
        open.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
        clear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
        setSize(400, 300);
    public void loadFile(){
        LoadingThread thread = new LoadingThread();
    public static void main(String[] args) {
        new ProgressBarDemo();
    class LoadingThread extends Thread {
        String text ="";
        int ch;
        int fileSize;
        int completedStatus;
        public LoadingThread() {
            try {
                file = new File(fileName.getText());
                dataInputStream = new DataInputStream(new FileInputStream(file));
                fileSize = (int)file.length();
                completedStatus = 0;
            } catch (Exception e) {
        public void run() {
            try {
                while ((ch = != -1) {
                    String text = ((char) ch +"");
            } catch (Exception e) {

Output :

Swing ProgressBar

In Progressing..

Swing ProgressBar Progress

After Completing :

Swing ProgressBar Complete

Happy Learning 🙂