Get and Set the Values of a JProgressBar in Java
Description
The following code shows how to get and Set the Values of a JProgressBar.
Example
import java.awt.BorderLayout;
//from w w w .ja va 2s .co m
import javax.swing.JFrame;
import javax.swing.JProgressBar;
public class Main extends JFrame {
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int minimum = 0;
int maximum = 100;
JProgressBar progress = new JProgressBar(minimum, maximum);
// Get the current value
int value = progress.getValue();
System.out.println(value);
// Get the minimum value
int min = progress.getMinimum();
System.out.println(min);
// Get the maximum value
int max = progress.getMaximum();
System.out.println(max);
// Change the minimum value
int newMin = 0;
progress.setMinimum(newMin);
// Change the maximum value
int newMax = 256;
progress.setMaximum(newMax);
// Set the value; the new value will be forced into the bar's range
int newValue = 33;
progress.setValue(newValue);
getContentPane().add(progress, BorderLayout.NORTH);
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 »