Java BoundedRangeModel .setExtent (int newExtent)
Syntax
BoundedRangeModel.setExtent(int newExtent) has the following syntax.
void setExtent(int newExtent)
Example
In the following code shows how to use BoundedRangeModel.setExtent(int newExtent) method.
import java.awt.BorderLayout;
/*from ww w .ja va 2s . co 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.setExtent(10);
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 »