Methods Summary |
---|
public void | addChangeListener(javax.swing.event.ChangeListener l)Adds a ChangeListener to the slider.
listenerList.add(ChangeListener.class, l);
|
private void | checkOrientation(int orientation)
switch (orientation) {
case VERTICAL:
case HORIZONTAL:
break;
default:
throw new IllegalArgumentException("orientation must be one of: VERTICAL, HORIZONTAL");
}
|
protected javax.swing.event.ChangeListener | createChangeListener()Subclasses that want to handle model ChangeEvents differently
can override this method to return their own ChangeListener
implementation. The default ChangeListener just forwards
ChangeEvents to the ChangeListeners added directly to the slider.
return new ModelListener();
|
public java.util.Hashtable | createStandardLabels(int increment)Creates a hashtable that will draw text labels starting at the
slider minimum using the increment specified.
If you call createStandardLabels( 10 )
and the slider minimum is
zero, then it will make labels for the values 0, 10, 20, 30, and so on.
return createStandardLabels( increment, getMinimum() );
|
public java.util.Hashtable | createStandardLabels(int increment, int start)Creates a hashtable that will draw text labels starting at the
start point
specified using the increment specified. If you call
createStandardLabels( 10, 2 ) ,
then it will make labels for the values 2, 12, 22, 32, and so on.
if ( start > getMaximum() || start < getMinimum() ) {
throw new IllegalArgumentException( "Slider label start point out of range." );
}
if ( increment <= 0 ) {
throw new IllegalArgumentException( "Label incremement must be > 0" );
}
class SmartHashtable extends Hashtable implements PropertyChangeListener {
int increment = 0;
int start = 0;
boolean startAtMin = false;
class LabelUIResource extends JLabel implements UIResource {
public LabelUIResource( String text, int alignment ) {
super( text, alignment );
setName("Slider.label");
}
}
public SmartHashtable( int increment, int start ) {
super();
this.increment = increment;
this.start = start;
startAtMin = start == getMinimum();
createLabels();
}
public void propertyChange( PropertyChangeEvent e ) {
if ( e.getPropertyName().equals( "minimum" ) && startAtMin ) {
start = getMinimum();
}
if ( e.getPropertyName().equals( "minimum" ) ||
e.getPropertyName().equals( "maximum" ) ) {
Enumeration keys = getLabelTable().keys();
Object key = null;
Hashtable hashtable = new Hashtable();
// Save the labels that were added by the developer
while ( keys.hasMoreElements() ) {
key = keys.nextElement();
Object value = getLabelTable().get( key );
if ( !(value instanceof LabelUIResource) ) {
hashtable.put( key, value );
}
}
clear();
createLabels();
// Add the saved labels
keys = hashtable.keys();
while ( keys.hasMoreElements() ) {
key = keys.nextElement();
put( key, hashtable.get( key ) );
}
((JSlider)e.getSource()).setLabelTable( this );
}
}
void createLabels() {
for ( int labelIndex = start; labelIndex <= getMaximum(); labelIndex += increment ) {
put( new Integer( labelIndex ), new LabelUIResource( ""+labelIndex, JLabel.CENTER ) );
}
}
}
SmartHashtable table = new SmartHashtable( increment, start );
if ( getLabelTable() != null && (getLabelTable() instanceof PropertyChangeListener) ) {
removePropertyChangeListener( (PropertyChangeListener)getLabelTable() );
}
addPropertyChangeListener( table );
return table;
|
protected void | fireStateChanged()Send a ChangeEvent, whose source is this Slider, to
each listener. This method method is called each time
a ChangeEvent is received from the model.
Object[] listeners = listenerList.getListenerList();
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i]==ChangeListener.class) {
if (changeEvent == null) {
changeEvent = new ChangeEvent(this);
}
((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
}
}
|
public javax.accessibility.AccessibleContext | getAccessibleContext()Gets the AccessibleContext associated with this JSlider.
For sliders, the AccessibleContext takes the form of an
AccessibleJSlider.
A new AccessibleJSlider instance is created if necessary.
if (accessibleContext == null) {
accessibleContext = new AccessibleJSlider();
}
return accessibleContext;
|
public javax.swing.event.ChangeListener[] | getChangeListeners()Returns an array of all the ChangeListener s added
to this JSlider with addChangeListener().
return (ChangeListener[])listenerList.getListeners(
ChangeListener.class);
|
public int | getExtent()Returns the "extent" -- the range of values "covered" by the knob.
return getModel().getExtent();
|
public boolean | getInverted()Returns true if the value-range shown for the slider is reversed,
return isInverted;
|
public java.util.Dictionary | getLabelTable()Returns the dictionary of what labels to draw at which values.
/*
if ( labelTable == null && getMajorTickSpacing() > 0 ) {
setLabelTable( createStandardLabels( getMajorTickSpacing() ) );
}
*/
return labelTable;
|
public int | getMajorTickSpacing()This method returns the major tick spacing. The number that is returned
represents the distance, measured in values, between each major tick mark.
If you have a slider with a range from 0 to 50 and the major tick spacing
is set to 10, you will get major ticks next to the following values:
0, 10, 20, 30, 40, 50.
return majorTickSpacing;
|
public int | getMaximum()Returns the maximum value supported by the slider.
return getModel().getMaximum();
|
public int | getMinimum()Returns the minimum value supported by the slider.
return getModel().getMinimum();
|
public int | getMinorTickSpacing()This method returns the minor tick spacing. The number that is returned
represents the distance, measured in values, between each minor tick mark.
If you have a slider with a range from 0 to 50 and the minor tick spacing
is set to 10, you will get minor ticks next to the following values:
0, 10, 20, 30, 40, 50.
return minorTickSpacing;
|
public javax.swing.BoundedRangeModel | getModel()Returns data model that handles the sliders three
fundamental properties: minimum, maximum, value.
return sliderModel;
|
public int | getOrientation()Return this slider's vertical or horizontal orientation.
return orientation;
|
public boolean | getPaintLabels()Tells if labels are to be painted.
return paintLabels;
|
public boolean | getPaintTicks()Tells if tick marks are to be painted.
return paintTicks;
|
public boolean | getPaintTrack()Tells if the track (area the slider slides in) is to be painted.
return paintTrack;
|
public boolean | getSnapToTicks()Returns true if the knob (and the data value it represents)
resolve to the closest tick mark next to where the user
positioned the knob.
return snapToTicks;
|
boolean | getSnapToValue()Returns true if the knob (and the data value it represents)
resolve to the closest slider value next to where the user
positioned the knob.
return snapToValue;
|
public javax.swing.plaf.SliderUI | getUI()Gets the UI object which implements the L&F for this component.
return(SliderUI)ui;
|
public java.lang.String | getUIClassID()Returns the name of the L&F class that renders this component.
return uiClassID;
|
public int | getValue()Returns the sliders value.
return getModel().getValue();
|
public boolean | getValueIsAdjusting()True if the slider knob is being dragged.
return getModel().getValueIsAdjusting();
|
protected java.lang.String | paramString()Returns a string representation of this JSlider. This method
is intended to be used only for debugging purposes, and the
content and format of the returned string may vary between
implementations. The returned string may be empty but may not
be null .
String paintTicksString = (paintTicks ?
"true" : "false");
String paintTrackString = (paintTrack ?
"true" : "false");
String paintLabelsString = (paintLabels ?
"true" : "false");
String isInvertedString = (isInverted ?
"true" : "false");
String snapToTicksString = (snapToTicks ?
"true" : "false");
String snapToValueString = (snapToValue ?
"true" : "false");
String orientationString = (orientation == HORIZONTAL ?
"HORIZONTAL" : "VERTICAL");
return super.paramString() +
",isInverted=" + isInvertedString +
",majorTickSpacing=" + majorTickSpacing +
",minorTickSpacing=" + minorTickSpacing +
",orientation=" + orientationString +
",paintLabels=" + paintLabelsString +
",paintTicks=" + paintTicksString +
",paintTrack=" + paintTrackString +
",snapToTicks=" + snapToTicksString +
",snapToValue=" + snapToValueString;
|
public void | removeChangeListener(javax.swing.event.ChangeListener l)Removes a ChangeListener from the slider.
listenerList.remove(ChangeListener.class, l);
|
public void | setExtent(int extent)Sets the size of the range "covered" by the knob. Most look
and feel implementations will change the value by this amount
if the user clicks on either side of the knob.
getModel().setExtent(extent);
|
public void | setInverted(boolean b)Specify true to reverse the value-range shown for the slider and false to
put the value range in the normal order. The order depends on the
slider's ComponentOrientation property. Normal (non-inverted)
horizontal sliders with a ComponentOrientation value of
LEFT_TO_RIGHT have their maximum on the right.
Normal horizontal sliders with a ComponentOrientation value of
RIGHT_TO_LEFT have their maximum on the left. Normal vertical
sliders have their maximum on the top. These labels are reversed when the
slider is inverted.
boolean oldValue = isInverted;
isInverted = b;
firePropertyChange("inverted", oldValue, isInverted);
if (b != oldValue) {
repaint();
}
|
public void | setLabelTable(java.util.Dictionary labels)Used to specify what label will be drawn at any given value.
The key-value pairs are of this format:
{ Integer value, java.swing.JComponent label } .
Dictionary oldTable = labelTable;
labelTable = labels;
updateLabelUIs();
firePropertyChange("labelTable", oldTable, labelTable );
if (labels != oldTable) {
revalidate();
repaint();
}
|
public void | setMajorTickSpacing(int n)This method sets the major tick spacing. The number that is passed-in
represents the distance, measured in values, between each major tick mark.
If you have a slider with a range from 0 to 50 and the major tick spacing
is set to 10, you will get major ticks next to the following values:
0, 10, 20, 30, 40, 50.
int oldValue = majorTickSpacing;
majorTickSpacing = n;
if ( labelTable == null && getMajorTickSpacing() > 0 && getPaintLabels() ) {
setLabelTable( createStandardLabels( getMajorTickSpacing() ) );
}
firePropertyChange("majorTickSpacing", oldValue, majorTickSpacing);
if (majorTickSpacing != oldValue && getPaintTicks()) {
repaint();
}
|
public void | setMaximum(int maximum)Sets the models maximum property.
int oldMax = getModel().getMaximum();
getModel().setMaximum(maximum);
firePropertyChange( "maximum", new Integer( oldMax ), new Integer( maximum ) );
|
public void | setMinimum(int minimum)Sets the models minimum property.
int oldMin = getModel().getMinimum();
getModel().setMinimum(minimum);
firePropertyChange( "minimum", new Integer( oldMin ), new Integer( minimum ) );
|
public void | setMinorTickSpacing(int n)This method sets the minor tick spacing. The number that is passed-in
represents the distance, measured in values, between each minor tick mark.
If you have a slider with a range from 0 to 50 and the minor tick spacing
is set to 10, you will get minor ticks next to the following values:
0, 10, 20, 30, 40, 50.
int oldValue = minorTickSpacing;
minorTickSpacing = n;
firePropertyChange("minorTickSpacing", oldValue, minorTickSpacing);
if (minorTickSpacing != oldValue && getPaintTicks()) {
repaint();
}
|
public void | setModel(javax.swing.BoundedRangeModel newModel)Sets the model that handles the sliders three
fundamental properties: minimum, maximum, value.
BoundedRangeModel oldModel = getModel();
if (oldModel != null) {
oldModel.removeChangeListener(changeListener);
}
sliderModel = newModel;
if (newModel != null) {
newModel.addChangeListener(changeListener);
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
(oldModel == null
? null : new Integer(oldModel.getValue())),
(newModel == null
? null : new Integer(newModel.getValue())));
}
}
firePropertyChange("model", oldModel, sliderModel);
|
public void | setOrientation(int orientation)Set the scrollbars orientation to either VERTICAL or HORIZONTAL.
checkOrientation(orientation);
int oldValue = this.orientation;
this.orientation = orientation;
firePropertyChange("orientation", oldValue, orientation);
if ((oldValue != orientation) && (accessibleContext != null)) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
((oldValue == VERTICAL)
? AccessibleState.VERTICAL : AccessibleState.HORIZONTAL),
((orientation == VERTICAL)
? AccessibleState.VERTICAL : AccessibleState.HORIZONTAL));
}
if (orientation != oldValue) {
revalidate();
}
|
public void | setPaintLabels(boolean b)Determines whether labels are painted on the slider.
boolean oldValue = paintLabels;
paintLabels = b;
if ( labelTable == null && getMajorTickSpacing() > 0 ) {
setLabelTable( createStandardLabels( getMajorTickSpacing() ) );
}
firePropertyChange("paintLabels", oldValue, paintLabels);
if (paintLabels != oldValue) {
revalidate();
repaint();
}
|
public void | setPaintTicks(boolean b)Determines whether tick marks are painted on the slider.
boolean oldValue = paintTicks;
paintTicks = b;
firePropertyChange("paintTicks", oldValue, paintTicks);
if (paintTicks != oldValue) {
revalidate();
repaint();
}
|
public void | setPaintTrack(boolean b)Determines whether the track is painted on the slider.
boolean oldValue = paintTrack;
paintTrack = b;
firePropertyChange("paintTrack", oldValue, paintTrack);
if (paintTrack != oldValue) {
repaint();
}
|
public void | setSnapToTicks(boolean b)Specifying true makes the knob (and the data value it represents)
resolve to the closest tick mark next to where the user
positioned the knob.
boolean oldValue = snapToTicks;
snapToTicks = b;
firePropertyChange("snapToTicks", oldValue, snapToTicks);
|
void | setSnapToValue(boolean b)Specifying true makes the knob (and the data value it represents)
resolve to the closest slider value next to where the user
positioned the knob. If the snapToTicks property has been set to
true, the snap-to-ticks behavior will prevail.
boolean oldValue = snapToValue;
snapToValue = b;
firePropertyChange("snapToValue", oldValue, snapToValue);
|
public void | setUI(javax.swing.plaf.SliderUI ui)Sets the UI object which implements the L&F for this component.
super.setUI(ui);
|
public void | setValue(int n)Sets the sliders current value. This method just forwards
the value to the model.
BoundedRangeModel m = getModel();
int oldValue = m.getValue();
if (oldValue == n) {
return;
}
m.setValue(n);
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
new Integer(oldValue),
new Integer(m.getValue()));
}
|
public void | setValueIsAdjusting(boolean b)Sets the models valueIsAdjusting property. Slider look and
feel implementations should set this property to true when
a knob drag begins, and to false when the drag ends. The
slider model will not generate ChangeEvents while
valueIsAdjusting is true.
BoundedRangeModel m = getModel();
boolean oldValue = m.getValueIsAdjusting();
m.setValueIsAdjusting(b);
if ((oldValue != b) && (accessibleContext != null)) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
((oldValue) ? AccessibleState.BUSY : null),
((b) ? AccessibleState.BUSY : null));
}
|
protected void | updateLabelUIs()Resets the UI property to a value from the current look and feel.
if ( getLabelTable() == null ) {
return;
}
Enumeration labels = getLabelTable().keys();
while ( labels.hasMoreElements() ) {
Object value = getLabelTable().get( labels.nextElement() );
if ( value instanceof JComponent ) {
JComponent component = (JComponent)value;
component.updateUI();
component.setSize( component.getPreferredSize() );
}
}
|
public void | updateUI()Resets the UI property to a value from the current look and feel.
updateLabelUIs();
setUI((SliderUI)UIManager.getUI(this));
|
private void | writeObject(java.io.ObjectOutputStream s)See readObject() and writeObject() in JComponent for more
information about serialization in Swing.
s.defaultWriteObject();
if (getUIClassID().equals(uiClassID)) {
byte count = JComponent.getWriteObjCounter(this);
JComponent.setWriteObjCounter(this, --count);
if (count == 0 && ui != null) {
ui.installUI(this);
}
}
|