下面列出了怎么用java.awt.event.AdjustmentEvent的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Listens to changes of the scroll bar of the text are showing the EULA text, enables the check
* box once the user scrolled to the end of the document.
*/
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
JScrollBar scrollBar = this.scrollPane.getVerticalScrollBar();
if (e.getSource() == scrollBar) {
// the maximum value of the scroll bar assumes that the content is
// not visible anymore, since this is not the case when scrolling
// to the end of the document (the last part is still visible),
// we have to include the visible amount in the comparison
int currentValue = scrollBar.getValue() + scrollBar.getVisibleAmount();
if (currentValue >= scrollBar.getMaximum()) {
// the user scrolled to the end of the document
this.acceptCheckBox.setEnabled(true);
this.acceptCheckBox.requestFocusInWindow();
}
}
}
void trackMouseDraggedScroll(int mouseX, int mouseY, int listWidth, int listHeight){
if (!mouseDraggedOutVertically){
if (vsb.beforeThumb(mouseX, mouseY)) {
vsb.setMode(AdjustmentEvent.UNIT_DECREMENT);
} else {
vsb.setMode(AdjustmentEvent.UNIT_INCREMENT);
}
}
if(!mouseDraggedOutVertically && (mouseY < 0 || mouseY >= listHeight)){
mouseDraggedOutVertically = true;
vsb.startScrollingInstance();
}
if (mouseDraggedOutVertically && mouseY >= 0 && mouseY < listHeight && mouseX >= 0 && mouseX < listWidth){
mouseDraggedOutVertically = false;
vsb.stopScrollingInstance();
}
}
/**
* Sets the value of this scrollbar to the specified value.
* <p>
* If the value supplied is less than the current minimum or
* greater than the current maximum, then one of those values is
* substituted, as appropriate. Also, creates and dispatches
* the AdjustementEvent with specified type and value.
*
* @param v the new value of the scrollbar
* @param type the type of the scrolling operation occurred
*/
private void setTypedValue(int v, int type) {
v = Math.max(v, minimum);
v = Math.min(v, maximum - visibleAmount);
if (v != value) {
value = v;
// Synchronously notify the listeners so that they are
// guaranteed to be up-to-date with the Adjustable before
// it is mutated again.
AdjustmentEvent e =
new AdjustmentEvent(this,
AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
type, value, isAdjusting);
adjustmentListener.adjustmentValueChanged(e);
}
}
void trackMouseDraggedScroll(int mouseX, int mouseY, int listWidth, int listHeight){
if (!mouseDraggedOutVertically){
if (vsb.beforeThumb(mouseX, mouseY)) {
vsb.setMode(AdjustmentEvent.UNIT_DECREMENT);
} else {
vsb.setMode(AdjustmentEvent.UNIT_INCREMENT);
}
}
if(!mouseDraggedOutVertically && (mouseY < 0 || mouseY >= listHeight)){
mouseDraggedOutVertically = true;
vsb.startScrollingInstance();
}
if (mouseDraggedOutVertically && mouseY >= 0 && mouseY < listHeight && mouseX >= 0 && mouseX < listWidth){
mouseDraggedOutVertically = false;
vsb.stopScrollingInstance();
}
}
void trackMouseDraggedScroll(int mouseX, int mouseY, int listWidth, int listHeight){
if (!mouseDraggedOutVertically){
if (vsb.beforeThumb(mouseX, mouseY)) {
vsb.setMode(AdjustmentEvent.UNIT_DECREMENT);
} else {
vsb.setMode(AdjustmentEvent.UNIT_INCREMENT);
}
}
if(!mouseDraggedOutVertically && (mouseY < 0 || mouseY >= listHeight)){
mouseDraggedOutVertically = true;
vsb.startScrollingInstance();
}
if (mouseDraggedOutVertically && mouseY >= 0 && mouseY < listHeight && mouseX >= 0 && mouseX < listWidth){
mouseDraggedOutVertically = false;
vsb.stopScrollingInstance();
}
}
/**
* Sets the value of this scrollbar to the specified value.
* <p>
* If the value supplied is less than the current minimum or
* greater than the current maximum, then one of those values is
* substituted, as appropriate. Also, creates and dispatches
* the AdjustementEvent with specified type and value.
*
* @param v the new value of the scrollbar
* @param type the type of the scrolling operation occured
*/
private void setTypedValue(int v, int type) {
v = Math.max(v, minimum);
v = Math.min(v, maximum - visibleAmount);
if (v != value) {
value = v;
// Synchronously notify the listeners so that they are
// guaranteed to be up-to-date with the Adjustable before
// it is mutated again.
AdjustmentEvent e =
new AdjustmentEvent(this,
AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
type, value, isAdjusting);
adjustmentListener.adjustmentValueChanged(e);
}
}
@Override
public void adjustmentValueChanged( final AdjustmentEvent event )
{
threshold = min + ( (log1001 - (float)Math.log10(1001-event.getValue()))/log1001 ) * (max-min);
label.setText( "Threshold = " + threshold );
if ( !isComputing )
{
updatePreview( ValueChange.THRESHOLD );
}
else if ( !event.getValueIsAdjusting() )
{
while ( isComputing )
{
SimpleMultiThreading.threadWait( 10 );
}
updatePreview( ValueChange.THRESHOLD );
}
}
void trackMouseDraggedScroll(int mouseX, int mouseY, int listWidth, int listHeight){
if (!mouseDraggedOutVertically){
if (vsb.beforeThumb(mouseX, mouseY)) {
vsb.setMode(AdjustmentEvent.UNIT_DECREMENT);
} else {
vsb.setMode(AdjustmentEvent.UNIT_INCREMENT);
}
}
if(!mouseDraggedOutVertically && (mouseY < 0 || mouseY >= listHeight)){
mouseDraggedOutVertically = true;
vsb.startScrollingInstance();
}
if (mouseDraggedOutVertically && mouseY >= 0 && mouseY < listHeight && mouseX >= 0 && mouseX < listWidth){
mouseDraggedOutVertically = false;
vsb.stopScrollingInstance();
}
}
/**
* Sets the value of this scrollbar to the specified value.
* <p>
* If the value supplied is less than the current minimum or
* greater than the current maximum, then one of those values is
* substituted, as appropriate. Also, creates and dispatches
* the AdjustementEvent with specified type and value.
*
* @param v the new value of the scrollbar
* @param type the type of the scrolling operation occurred
*/
private void setTypedValue(int v, int type) {
v = Math.max(v, minimum);
v = Math.min(v, maximum - visibleAmount);
if (v != value) {
value = v;
// Synchronously notify the listeners so that they are
// guaranteed to be up-to-date with the Adjustable before
// it is mutated again.
AdjustmentEvent e =
new AdjustmentEvent(this,
AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
type, value, isAdjusting);
adjustmentListener.adjustmentValueChanged(e);
}
}
void trackMouseDraggedScroll(int mouseX, int mouseY, int listWidth, int listHeight){
if (!mouseDraggedOutVertically){
if (vsb.beforeThumb(mouseX, mouseY)) {
vsb.setMode(AdjustmentEvent.UNIT_DECREMENT);
} else {
vsb.setMode(AdjustmentEvent.UNIT_INCREMENT);
}
}
if(!mouseDraggedOutVertically && (mouseY < 0 || mouseY >= listHeight)){
mouseDraggedOutVertically = true;
vsb.startScrollingInstance();
}
if (mouseDraggedOutVertically && mouseY >= 0 && mouseY < listHeight && mouseX >= 0 && mouseX < listWidth){
mouseDraggedOutVertically = false;
vsb.stopScrollingInstance();
}
}
void trackMouseDraggedScroll(int mouseX, int mouseY, int listWidth, int listHeight){
if (!mouseDraggedOutVertically){
if (vsb.beforeThumb(mouseX, mouseY)) {
vsb.setMode(AdjustmentEvent.UNIT_DECREMENT);
} else {
vsb.setMode(AdjustmentEvent.UNIT_INCREMENT);
}
}
if(!mouseDraggedOutVertically && (mouseY < 0 || mouseY >= listHeight)){
mouseDraggedOutVertically = true;
vsb.startScrollingInstance();
}
if (mouseDraggedOutVertically && mouseY >= 0 && mouseY < listHeight && mouseX >= 0 && mouseX < listWidth){
mouseDraggedOutVertically = false;
vsb.stopScrollingInstance();
}
}
@Override
public void adjustmentValueChanged( final AdjustmentEvent event )
{
threshold = min + ( (log1001 - (float)Math.log10(1001-event.getValue()))/log1001 ) * (max-min);
label.setText( "Threshold = " + threshold );
if ( !isComputing )
{
updatePreview( ValueChange.THRESHOLD );
}
else if ( !event.getValueIsAdjusting() )
{
while ( isComputing )
{
SimpleMultiThreading.threadWait( 10 );
}
updatePreview( ValueChange.THRESHOLD );
}
}
@Override
public void adjustmentValueChanged(final AdjustmentEvent evt) {
if (!scrollBarsInitialized) {
return;
}
// If this is not done, mousePressed events accumilate
// and the result is that scrolling doesn't stop after
// the mouse is released
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if (evt.getAdjustable() == vertical) {
setFirstLine(vertical.getValue());
} else {
setHorizontalOffset(-horizontal.getValue());
}
}
});
}
void dragEnd(final int value) {
final Scrollbar sb = (Scrollbar)target;
if (!dragInProgress) {
return;
}
dragInProgress = false;
WToolkit.executeOnEventHandlerThread(sb, new Runnable() {
public void run() {
// NB: notification only, no sb.setValue()
// last TRACK event will have done it already
sb.setValueIsAdjusting(false);
postEvent(new AdjustmentEvent(sb,
AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
AdjustmentEvent.TRACK, value, false));
}
});
}
public void stateChanged(ChangeEvent e) {
Object obj = e.getSource();
if (obj instanceof BoundedRangeModel) {
int id = AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED;
int type = AdjustmentEvent.TRACK;
BoundedRangeModel model = (BoundedRangeModel)obj;
int value = model.getValue();
boolean isAdjusting = model.getValueIsAdjusting();
fireAdjustmentValueChanged(id, type, value, isAdjusting);
}
}
public void stateChanged(ChangeEvent e) {
Object obj = e.getSource();
if (obj instanceof BoundedRangeModel) {
int id = AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED;
int type = AdjustmentEvent.TRACK;
BoundedRangeModel model = (BoundedRangeModel)obj;
int value = model.getValue();
boolean isAdjusting = model.getValueIsAdjusting();
fireAdjustmentValueChanged(id, type, value, isAdjusting);
}
}
/**
* Creates a handler for scrolling for the view and its surrounding scrollPane.
*
* @param view
* the {@link ProcessRendererView}
* @param scrollPane
* the {@link JScrollPane} containing the view
*/
ProcessPanelScroller(final ProcessRendererView view, final JScrollPane scrollPane) {
this.scrollPane = scrollPane;
this.rendererView = view;
rendererView.addMouseWheelListener(wheelListener);
// add listener to check for size changes of the scrollbar
scrollPane.getHorizontalScrollBar().addAdjustmentListener(new AdjustmentListener() {
private int lastValue;
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
if (lastValue == scrollPane.getHorizontalScrollBar().getMaximum()) {
return;
}
// the maximum value changed, so the process canvas size changed
lastValue = scrollPane.getHorizontalScrollBar().getMaximum();
if (zoomed) {
// set the scrollbar values needed for centered zoom if needed
if (scrollPane.getHorizontalScrollBar().getValue() != desiredHorizontalScrollValue) {
scrollPane.getHorizontalScrollBar().setValue(desiredHorizontalScrollValue);
}
if (scrollPane.getVerticalScrollBar().getValue() != desiredVerticalScrollValue) {
scrollPane.getVerticalScrollBar().setValue(desiredVerticalScrollValue);
}
zoomed = false;
}
}
});
}
private void initComponents() {
this.setPreferredSize(new Dimension(100, 200));
this.setLayout(new BorderLayout());
this.setBackground(Color.white);
_vScrollBar = new JScrollBar(JScrollBar.VERTICAL);
_vScrollBar.addAdjustmentListener(new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
onScrollValueChanged(e);
}
});
this.add(_vScrollBar, BorderLayout.EAST);
this._vScrollBar.setSize(20, this.getHeight());
this._vScrollBar.setLocation(this.getWidth() - this._vScrollBar.getWidth(), 0);
_textField = new JTextField();
_textField.setVisible(false);
this.add(_textField);
_frmPointSymbolSet = null;
_frmPolylineSymbolSet = null;
_frmPolygonSymbolSet = null;
_frmColorSymbolSet = null;
_legendScheme = null;
_breakHeight = 20;
_symbolWidth = 60;
_valueWidth = (this.getWidth() - _symbolWidth) / 2;
_labelWidth = _valueWidth;
}
@Override
public void adjustmentValueChanged( final AdjustmentEvent event )
{
radius1 = Math.round( computeValueFromScrollbarPosition( event.getValue(), min, max, scrollbarSize ) );
if ( !enableRadius2 )
{
radius2 = computeRadius2( radius1 );
radiusText2.setText( "Radius 2 = " + radius2 );
radiusScrollbar2.setValue( computeScrollbarPositionFromValue( radius2, min, max, scrollbarSize ) );
}
else if ( radius1 >= radius2 )
{
radius1 = radius2 - 2;
radiusScrollbar1.setValue( computeScrollbarPositionFromValue( radius1, min, max, scrollbarSize ) );
}
label.setText( "Radius 1 = " + radius1 );
if ( !event.getValueIsAdjusting() )
{
while ( isComputing )
{
SimpleMultiThreading.threadWait( 10 );
}
updatePreview( ValueChange.RADIUS );
}
}
public void stateChanged(ChangeEvent e) {
Object obj = e.getSource();
if (obj instanceof BoundedRangeModel) {
int id = AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED;
int type = AdjustmentEvent.TRACK;
BoundedRangeModel model = (BoundedRangeModel)obj;
int value = model.getValue();
boolean isAdjusting = model.getValueIsAdjusting();
fireAdjustmentValueChanged(id, type, value, isAdjusting);
}
}
@Override
public void adjustmentValueChanged(final AdjustmentEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
checkScrollBar(e);
}
});
}
public static void main(String[] argv) {
HNav nav0 = new HNav(200);
nav0.setMaximum(100);
nav0.setMinimum(-100);
HNav nav1 = new HNav(200);
nav1.setMaximum(0);
nav1.setMinimum(-100);
HNav nav2 = new HNav(200);
nav2.setMaximum(100);
nav2.setMinimum(0);
AdjustmentListener l = new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
System.out.println(e);
}
};
nav0.addAdjustmentListener(l);
nav1.addAdjustmentListener(l);
nav2.addAdjustmentListener(l);
new Frame(nav0, true);
new Frame(nav1, true);
new Frame(nav2, true);
}
/**
* Sets the <code>valueIsAdjusting</code> property.
*
* @param b new adjustment-in-progress status
* @see #getValueIsAdjusting
* @since 1.4
*/
public void setValueIsAdjusting(boolean b) {
if (isAdjusting != b) {
isAdjusting = b;
AdjustmentEvent e =
new AdjustmentEvent(this,
AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
AdjustmentEvent.TRACK, value, b);
adjustmentListener.adjustmentValueChanged(e);
}
}
private void postAdjustmentEvent(final int type, final int value,
final boolean isAdjusting)
{
final Scrollbar sb = (Scrollbar)target;
WToolkit.executeOnEventHandlerThread(sb, new Runnable() {
public void run() {
sb.setValueIsAdjusting(isAdjusting);
sb.setValue(value);
postEvent(new AdjustmentEvent(sb,
AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
type, value, isAdjusting));
}
});
}
@Override
public void adjustmentValueChanged(final AdjustmentEvent e) {
final int value = e.getValue();
synchronized (getDelegateLock()) {
if (currentValue == value) {
return;
}
currentValue = value;
}
getTarget().setValueIsAdjusting(e.getValueIsAdjusting());
getTarget().setValue(value);
postEvent(new AdjustmentEvent(getTarget(), e.getID(),
e.getAdjustmentType(), value,
e.getValueIsAdjusting()));
}
/**
* Sets the <code>valueIsAdjusting</code> property.
*
* @param b new adjustment-in-progress status
* @see #getValueIsAdjusting
* @since 1.4
*/
public void setValueIsAdjusting(boolean b) {
if (isAdjusting != b) {
isAdjusting = b;
AdjustmentEvent e =
new AdjustmentEvent(this,
AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
AdjustmentEvent.TRACK, value, b);
adjustmentListener.adjustmentValueChanged(e);
}
}
private void postAdjustmentEvent(final int type, final int value,
final boolean isAdjusting)
{
final Scrollbar sb = (Scrollbar)target;
WToolkit.executeOnEventHandlerThread(sb, new Runnable() {
public void run() {
sb.setValueIsAdjusting(isAdjusting);
sb.setValue(value);
postEvent(new AdjustmentEvent(sb,
AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
type, value, isAdjusting));
}
});
}
public void stateChanged(ChangeEvent e) {
Object obj = e.getSource();
if (obj instanceof BoundedRangeModel) {
int id = AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED;
int type = AdjustmentEvent.TRACK;
BoundedRangeModel model = (BoundedRangeModel)obj;
int value = model.getValue();
boolean isAdjusting = model.getValueIsAdjusting();
fireAdjustmentValueChanged(id, type, value, isAdjusting);
}
}
/**
* Sets the <code>valueIsAdjusting</code> property.
*
* @param b new adjustment-in-progress status
* @see #getValueIsAdjusting
* @since 1.4
*/
public void setValueIsAdjusting(boolean b) {
if (isAdjusting != b) {
isAdjusting = b;
AdjustmentEvent e =
new AdjustmentEvent(this,
AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
AdjustmentEvent.TRACK, value, b);
adjustmentListener.adjustmentValueChanged(e);
}
}
/**
* Sets the <code>valueIsAdjusting</code> property.
*
* @param b new adjustment-in-progress status
* @see #getValueIsAdjusting
* @since 1.4
*/
public void setValueIsAdjusting(boolean b) {
if (isAdjusting != b) {
isAdjusting = b;
AdjustmentEvent e =
new AdjustmentEvent(this,
AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
AdjustmentEvent.TRACK, value, b);
adjustmentListener.adjustmentValueChanged(e);
}
}