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. Following is the syntax.
[java]public class JProgressBar extends
JComponent implements
SwingConstants, Accessible[/java]
Example
Create the package and import Swing containers to inherit the properties and methods.
[java]package swing;
import java.awt.Container;[/java]
Create a class ProgressBar that should extend JFrame, ProgressBar will hire the properties from the JFrame.
[java]public class ProgressBar extends JFrame[/java]
Create required events i.e create timer to check the status, create ProgressBar to scroll from starting to ending, Create button such as start or stop.
[java]private Timer timer;
private JProgressBar pbar;
private JButton sBtn;[/java]
Create a constructor that should be same as class name and create the method inside the constructor.
[java]public ProgressBar() {
initUI();
}[/java]
Call the created method and create object to Progress bar, add paint to the progress scrolling status.
[java]pbar = new JProgressBar();
pbar.setStringPainted(true);[/java]
Create object to the start button and implement ActionInterface interface. When user click on start button, action will be performed.
[java]sBtn = new JButton("Start");
sBtn.addActionListener(new ClickAction());[/java]
Add listener to the timer that is UpdateBarListener().
[java]timer = new Timer(50, new UpdateBarListener());[/java]
Create layout to the ProgressBar and start button and create title to the window, add termination method to the window.
[java]createLayout(pbar, sBtn);
setTitle("JProgressBar");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);[/java]
Create the group layout.
[java]Container pane = getContentPane();
GroupLayout gl = new GroupLayout(pane);
pane.setLayout(gl); [/java]
Turn on gaps between the components automatically.
[java]gl.setAutoCreateContainerGaps(true);[/java]
Create the sequential group at horizontal axis and create parallel group at vertical axis.
[java]gl.setHorizontalGroup(gl.createSequentialGroup()
.addComponent(arg[0])
.addComponent(arg[1])
);
gl.setVerticalGroup(gl.createParallelGroup(CENTER)
.addComponent(arg[0])
.addComponent(arg[1])
); [/java]
Implement ActionListener on start button and set the percent to 100. If status crosses the 100 then automatically terminates program.
[java] 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); [/java]
ProgressBar.java
[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);
}
});
}
}[/java]
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.