A scrollbar in a scroll pane fires adjustment events whenever its value changes.
// Create a scrollable text area
JTextArea textArea = new JTextArea();
JScrollPane pane = new JScrollPane(textArea); // Listen for value changes in the scroll pane's scrollbars
AdjustmentListener listener = new MyAdjustmentListener();
pane.getHorizontalScrollBar().addAdjustmentListener(listener);
pane.getVerticalScrollBar().addAdjustmentListener(listener); class MyAdjustmentListener implements AdjustmentListener {
// This method is called whenever the value of a scrollbar is changed,
// either by the user or programmatically.
public void adjustmentValueChanged(AdjustmentEvent evt) {
Adjustable source = evt.getAdjustable(); // getValueIsAdjusting() returns true if the user is currently
// dragging the scrollbar's knob and has not picked a final value
if (evt.getValueIsAdjusting()) {
// The user is dragging the knob
return;
} // Determine which scrollbar fired the event
int orient = source.getOrientation();
if (orient == Adjustable.HORIZONTAL) {
// Event from horizontal scrollbar
} else {
// Event from vertical scrollbar
} // Determine the type of event
int type = evt.getAdjustmentType();
switch (type) {
case AdjustmentEvent.UNIT_INCREMENT:
// Scrollbar was increased by one unit
break;
case AdjustmentEvent.UNIT_DECREMENT:
// Scrollbar was decreased by one unit
break;
case AdjustmentEvent.BLOCK_INCREMENT:
// Scrollbar was increased by one block
break;
case AdjustmentEvent.BLOCK_DECREMENT:
// Scrollbar was decreased by one block
break;
case AdjustmentEvent.TRACK:
// The knob on the scrollbar was dragged
break;
} // Get current value
int value = evt.getValue();
}
}
Related Examples |