jprogressbar
In Java, JProgressBar is a Swing component that provides visual feedback to the user about the progress of a long-running operation. It can be used to display the progress of a file download, an installation process, or any other long-running task.
Here is an example of how to use JProgressBar:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ProgressBarExample extends JFrame implements ActionListener {
private JProgressBar progressBar;
private JButton startButton;
private Timer timer;
private int progress;
public ProgressBarExample() {
progressBar = new JProgressBar(0, 100);
progressBar.setValue(0);
progressBar.setStringPainted(true);
startButton = new JButton("Start");
startButton.addActionListener(this);
JPanel panel = new JPanel(new BorderLayout());
panel.add(progressBar, BorderLayout.CENTER);
panel.add(startButton, BorderLayout.SOUTH);
setContentPane(panel);
setSize(300, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
timer = new Timer(100, this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startButton) {
startButton.setEnabled(false);
timer.start();
} else if (e.getSource() == timer) {
progress += 5;
if (progress > 100) {
progress = 0;
startButton.setEnabled(true);
timer.stop();
}
progressBar.setValue(progress);
}
}
public static void main(String[] args) {
new ProgressBarExample();
}
}
In this example, a JProgressBar is created with a minimum value of 0 and a maximum value of 100. The StringPainted property is set to true to display the progress percentage as text on the progress bar. A JButton is also created to start the progress.
When the user clicks the "Start" button, the button is disabled and a Timer is started with an interval of 100 milliseconds. The timer increments the progress value by 5 every 100 milliseconds until it reaches 100, at which point the progress bar is reset to 0 and the "Start" button is re-enabled.
This is just a basic example, but JProgressBar can be used in many different ways to provide visual feedback to the user about the progress of long-running tasks.
