SwingWorker Progress Bar example is creating progress bar and it closes the progressbar automatically when it's done.
We need to provide the progress bar while running the application to increase the user-friendly.
In this program we are building the progress bar with Swing framework.
This program indicates how much task is completed and same thing shows in the progressbar.
Source code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
public class MyProgressBarTest extends JFrame {
private static final long serialVersionUID = 1L;
private static JProgressBar progressBar;
private static JLabel waitLabel = new JLabel("Please wait...");
public static void main(String[] args) {
MyProgressBarTest obj = new MyProgressBarTest();
obj.createGUI();
}
public void createGUI() {
JPanel panel = new JPanel();
JButton button = new JButton("Progress");
progressBar = new JProgressBar();
progressBar.setVisible(false);
waitLabel.setVisible(false);
panel.add(waitLabel);
panel.add(progressBar);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
SwingWorker<Void, Void> mySwingWorker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
// mimic some long-running process here...
calculateResult();
return null;
}
//actual code is running on backend of progress bar
public void calculateResult() {
for (int i = 0; i < 300000; i++) {
System.out.println("Progress Bar: " + i);
}
}
//disable the progressbar and waitlabel when task completed
protected void done() {
progressBar.setVisible(false);
waitLabel.setVisible(false);
}
};
mySwingWorker.execute();
progressBar.setIndeterminate(true);
progressBar.setVisible(true);
waitLabel.setVisible(true);
}
});
panel.add(button);
add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
pack();
setSize(200, 300);
setVisible(true);
}
}
We need to provide the progress bar while running the application to increase the user-friendly.
In this program we are building the progress bar with Swing framework.
This program indicates how much task is completed and same thing shows in the progressbar.
Source code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
public class MyProgressBarTest extends JFrame {
private static final long serialVersionUID = 1L;
private static JProgressBar progressBar;
private static JLabel waitLabel = new JLabel("Please wait...");
public static void main(String[] args) {
MyProgressBarTest obj = new MyProgressBarTest();
obj.createGUI();
}
public void createGUI() {
JPanel panel = new JPanel();
JButton button = new JButton("Progress");
progressBar = new JProgressBar();
progressBar.setVisible(false);
waitLabel.setVisible(false);
panel.add(waitLabel);
panel.add(progressBar);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
SwingWorker<Void, Void> mySwingWorker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
// mimic some long-running process here...
calculateResult();
return null;
}
//actual code is running on backend of progress bar
public void calculateResult() {
for (int i = 0; i < 300000; i++) {
System.out.println("Progress Bar: " + i);
}
}
//disable the progressbar and waitlabel when task completed
protected void done() {
progressBar.setVisible(false);
waitLabel.setVisible(false);
}
};
mySwingWorker.execute();
progressBar.setIndeterminate(true);
progressBar.setVisible(true);
waitLabel.setVisible(true);
}
});
panel.add(button);
add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
pack();
setSize(200, 300);
setVisible(true);
}
}
No comments:
Post a Comment