Introduction
In this article, we describe a very popular or common feature of any software installation. I think all of you have seen progress bars. Now we are describing how we can make your own progress bars. A Progress Bar is a component used to show the status of the application; how much time is to complete the task of the application.
Sometimes a task running within a program might take a while to complete. A user-friendly program provides some indication to the user that the task is occurring, how long the task might take, and how much work has already been done. One way of indicating work, and perhaps the amount of progress, is to use an animated image.
Creating progress bar in Java
Step 1: Importing a necessary package.
- import java.util.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.*;
Step 2: Defining the necessary variables.
- JProgressBar prbr;
- JButton b;
- JLabel l;
- JPanel P;
Step 3: Create the components within the constructor (its optional), but it is not necessary to define them in the constructor; you can define then anywhere etc.
- public MyProgressBar()
- {
-
- P = new JPanel();
-
- P.setPreferredSize(new Dimension(400, 400));
-
- getContentPane().add(P);
-
- l = new JLabel("Waiting for your click...");
-
- l.setPreferredSize(new Dimension(380, 45));
-
- P.add(l);
-
- prbr = new JProgressBar();
-
- prbr.setPreferredSize(new Dimension(300, 20));
-
- prbr.setMinimum(0);
-
- prbr.setMaximum(1000);
-
- prbr.setValue(0);
-
- prbr.setBounds(30, 50, 360, 30);
-
- P.add(prbr);
-
- b = new JButton("Go");
- P.add(b);
- b.addActionListener(this);
- setTitle("MY Progress Bar ready");
- setSize(400, 400);
- setBackground(Color.Green);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
Step 4: Override the action performed method.
- public void actionPerformed(ActionEvent event)
- {
- if (event.getSource() == b)
- {
-
- b.setEnabled(false);
-
- for (int iCtr = 1; iCtr < 1001; iCtr++)
- {
-
- DoTask(iCtr);
-
- l.setText("Performing task " + iCtr + " of 1000");
- Rectangle labelRect = l.getBounds();
- labelRect.x = 0;
- labelRect.y = 0;
- l.paintImmediately(labelRect);
- prbr.setValue(iCtr);
- Rectangle progressRect = prbr.getBounds();
- progressRect.x = 0;
- progressRect.y = 0;
- prbr.paintImmediately(progressRect);
- }
- }
- }
Step 5: Create the Dotask method functionality.
- public void DoTask(int iCtr)
- {
- Random random = new Random(iCtr);
-
- for (int iValue = 0; iValue < random.nextFloat() * 1000; iValue++)
- {
- System.out.println("Value=" + iValue);
- }
- }
Step 6: Create the main method and create an object of this class within main.
- public static void main(String args[])
- {
-
- MyProgressBar mF = new MyProgressBar();
- mF.setVisible(true);
- mF.pack();
- }
Complete Code
- import java.util.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.*;
- class MyProgressBar extends JFrame implements ActionListener
- {
- JProgressBar prbr;
- JButton b;
- JLabel l;
- JPanel P;
- public MyProgressBar()
- {
- setTitle("MY Progress Bar ready");
- setSize(400, 400);
- setBackground(Color.green);
- P = new JPanel();
- P.setPreferredSize(new Dimension(400, 400));
- getContentPane().add(P);
-
- l = new JLabel("Waiting to start tasks...");
- l.setPreferredSize(new Dimension(380, 45));
- P.add(l);
- prbr = new JProgressBar();
- prbr.setPreferredSize(new Dimension(300, 20));
- prbr.setMinimum(0);
- prbr.setMaximum(1000);
- prbr.setValue(0);
- prbr.setBounds(30, 50, 360, 30);
- P.add(prbr);
- b = new JButton("Go");
- P.add(b);
- b.addActionListener(this);
- }
- public void actionPerformed(ActionEvent event)
- {
- if (event.getSource() == b)
- {
-
- b.setEnabled(false);
-
- for (int iCtr = 1; iCtr < 1001; iCtr++)
- {
-
- DoBogusTask(iCtr);
-
- l.setText("Performing task " + iCtr + " of 1000");
- Rectangle labelRect = l.getBounds();
- labelRect.x = 0;
- labelRect.y = 0;
- l.paintImmediately(labelRect);
- prbr.setValue(iCtr);
- Rectangle progressRect = prbr.getBounds();
- progressRect.x = 0;
- progressRect.y = 0;
- prbr.paintImmediately(progressRect);
- }
- }
- }
- public void DoBogusTask(int iCtr)
- {
- Random random = new Random(iCtr);
-
- for (int iValue = 0; iValue < random.nextFloat() * 1000; iValue++)
- {
- System.out.println("iValue=" + iValue);
- }
- }
- public static void main(String args[])
- {
-
- MyProgressBar mF = new MyProgressBar();
- mF.setVisible(true);
- mF.pack();
- }
- }
Output
Cmd output
The initial output of the frame with progress bar.
After clicking on the go button:
Resources