Code for a progress bar
Details:
This code initialises and pops up a progress bar. You should call initProgressBar ()
at the start of where you want the progress bar, then the progress value can be altered by
setting the value at relevant points, e.g. pb.setValue(25);
. Finally, call
dialog.dispose();
at the end to get rid of the bar.
Original author/s: Marc Mettes
Original location/s: Java: A JDialog and JProgressBar Example
License: Creative Commons Attribution License 3.0
Imports and instance variables
import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JProgressBar; private JProgressBar pb = new JProgressBar(0,100); private JDialog dialog;
Code
public void initProgressBar () { pb.setPreferredSize(new Dimension(175,20)); pb.setString("Working"); pb.setStringPainted(true); pb.setValue(0); JLabel label = new JLabel("Progress: "); JPanel center_panel = new JPanel(); center_panel.add(label); center_panel.add(pb); dialog = new JDialog((Frame)null, "Working ..."); dialog.getContentPane().add(center_panel, BorderLayout.CENTER); dialog.pack(); dialog.setVisible(true); dialog.setLocationRelativeTo(null); // center on screen dialog.toFront(); // raise above other java windows }