Set progress value for JProgressBar in Java
Description
The following code shows how to set progress value for JProgressBar.
Example
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//from w w w . jav a 2s . c o m
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
public class Main extends JFrame {
JProgressBar bar = new JProgressBar();
JButton step = new JButton("Step");
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
step.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int value = bar.getValue() + 7;
if (value > bar.getMaximum()) {
value = bar.getMaximum();
}
bar.setValue(value);
}
});
getContentPane().add(bar, BorderLayout.NORTH);
getContentPane().add(step, BorderLayout.EAST);
pack();
setVisible(true);
}
public static void main(String arg[]) {
new Main();
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »