Java BoundedRangeModel .setValue (int newValue)
Syntax
BoundedRangeModel.setValue(int newValue) has the following syntax.
void setValue(int newValue)
Example
In the following code shows how to use BoundedRangeModel.setValue(int newValue) method.
import java.awt.BorderLayout;
// w w w . ja v a2s. c o m
import javax.swing.BoundedRangeModel;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JScrollBar;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Main {
public static void main(String args[]) throws Exception {
ChangeListener changeListener = new BoundedChangeListener();
JScrollBar anotherJScrollBar = new JScrollBar(JScrollBar.HORIZONTAL);
BoundedRangeModel model = anotherJScrollBar.getModel();
model.addChangeListener(changeListener);
model.setValue(1);
JFrame frame = new JFrame("ScrollBars R Us");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(anotherJScrollBar, BorderLayout.NORTH);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
class BoundedChangeListener implements ChangeListener {
public void stateChanged(ChangeEvent changeEvent) {
Object source = changeEvent.getSource();
if (source instanceof BoundedRangeModel) {
BoundedRangeModel aModel = (BoundedRangeModel) source;
if (!aModel.getValueIsAdjusting()) {
System.out.println("Changed: " + aModel.getValue());
}
} else if (source instanceof JSlider) {
JSlider theJSlider = (JSlider) source;
if (!theJSlider.getValueIsAdjusting()) {
System.out.println("Slider changed: " + theJSlider.getValue());
}
} else if (source instanceof JProgressBar) {
JProgressBar theJProgressBar = (JProgressBar) source;
System.out.println("ProgressBar changed: " + theJProgressBar.getValue());
} else {
System.out.println("Something changed: " + source);
}
}
}
Home »
Java Tutorial »
javax.swing »
Java Tutorial »
javax.swing »