| Method from javax.swing.JSlider Detail: |
public void addChangeListener(ChangeListener l) {
listenerList.add(ChangeListener.class, l);
}
Adds a ChangeListener to the slider. |
protected ChangeListener createChangeListener() {
return new ModelListener();
}
Subclasses that want to handle {@code ChangeEvent}s
from the model differently
can override this to return
an instance of a custom ChangeListener implementation.
The default {@code ChangeListener} simply calls the
{@code fireStateChanged} method to forward {@code ChangeEvent}s
to the {@code ChangeListener}s that have been added directly to the
slider. |
public Hashtable createStandardLabels(int increment) {
return createStandardLabels( increment, getMinimum() );
}
Creates a {@code Hashtable} of numerical text labels, starting at the
slider minimum, and using the increment specified.
For example, if you call createStandardLabels( 10 )
and the slider minimum is zero,
then labels will be created for the values 0, 10, 20, 30, and so on.
For the labels to be drawn on the slider, the returned {@code Hashtable}
must be passed into {@code setLabelTable}, and {@code setPaintLabels}
must be set to {@code true}.
For further details on the makeup of the returned {@code Hashtable}, see
the {@code setLabelTable} documentation. |
public Hashtable createStandardLabels(int increment,
int start) {
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< Object, Object > 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 Font getFont() {
Font font = super.getFont();
if (font != null && !(font instanceof UIResource)) {
return font;
}
return JSlider.this.getFont();
}
public Color getForeground() {
Color fg = super.getForeground();
if (fg != null && !(fg instanceof UIResource)) {
return fg;
}
if (!(JSlider.this.getForeground() instanceof UIResource)) {
return JSlider.this.getForeground();
}
return fg;
}
}
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();
Hashtable< Object, Object > hashtable = new Hashtable< Object, Object >();
// Save the labels that were added by the developer
while ( keys.hasMoreElements() ) {
Object key = keys.nextElement();
Object value = labelTable.get(key);
if ( !(value instanceof LabelUIResource) ) {
hashtable.put( key, value );
}
}
clear();
createLabels();
// Add the saved labels
keys = hashtable.keys();
while ( keys.hasMoreElements() ) {
Object key = keys.nextElement();
put( key, hashtable.get( key ) );
}
((JSlider)e.getSource()).setLabelTable( this );
}
}
void createLabels() {
for ( int labelIndex = start; labelIndex < = getMaximum(); labelIndex += increment ) {
put( Integer.valueOf( labelIndex ), new LabelUIResource( ""+labelIndex, JLabel.CENTER ) );
}
}
}
SmartHashtable table = new SmartHashtable( increment, start );
Dictionary labelTable = getLabelTable();
if (labelTable != null && (labelTable instanceof PropertyChangeListener)) {
removePropertyChangeListener((PropertyChangeListener) labelTable);
}
addPropertyChangeListener( table );
return table;
}
Creates a {@code Hashtable} of numerical text labels, starting at the
starting point specified, and using the increment specified.
For example, if you call
createStandardLabels( 10, 2 ),
then labels will be created for the values 2, 12, 22, 32, and so on.
For the labels to be drawn on the slider, the returned {@code Hashtable}
must be passed into {@code setLabelTable}, and {@code setPaintLabels}
must be set to {@code true}.
For further details on the makeup of the returned {@code Hashtable}, see
the {@code setLabelTable} documentation. |
protected void fireStateChanged() {
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);
}
}
}
Send a {@code ChangeEvent}, whose source is this {@code JSlider}, to
all {@code ChangeListener}s that have registered interest in
{@code ChangeEvent}s.
This method is called each time a {@code ChangeEvent} is received from
the model.
The event instance is created if necessary, and stored in
{@code changeEvent}. |
public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = new AccessibleJSlider();
}
return accessibleContext;
}
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. |
public ChangeListener[] getChangeListeners() {
return listenerList.getListeners(ChangeListener.class);
}
Returns an array of all the ChangeListeners added
to this JSlider with addChangeListener(). |
public int getExtent() {
return getModel().getExtent();
}
Returns the "extent" from the BoundedRangeModel.
This respresents the range of values "covered" by the knob. |
public boolean getInverted() {
return isInverted;
}
Returns true if the value-range shown for the slider is reversed, |
public Dictionary getLabelTable() {
/*
if ( labelTable == null && getMajorTickSpacing() > 0 ) {
setLabelTable( createStandardLabels( getMajorTickSpacing() ) );
}
*/
return labelTable;
}
Returns the dictionary of what labels to draw at which values. |
public int getMajorTickSpacing() {
return majorTickSpacing;
}
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. |
public int getMaximum() {
return getModel().getMaximum();
}
Returns the maximum value supported by the slider
from the BoundedRangeModel. |
public int getMinimum() {
return getModel().getMinimum();
}
Returns the minimum value supported by the slider
from the BoundedRangeModel. |
public int getMinorTickSpacing() {
return minorTickSpacing;
}
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. |
public BoundedRangeModel getModel() {
return sliderModel;
}
Returns the {@code BoundedRangeModel} that handles the slider's three
fundamental properties: minimum, maximum, value. |
public int getOrientation() {
return orientation;
}
Return this slider's vertical or horizontal orientation. |
public boolean getPaintLabels() {
return paintLabels;
}
Tells if labels are to be painted. |
public boolean getPaintTicks() {
return paintTicks;
}
Tells if tick marks are to be painted. |
public boolean getPaintTrack() {
return paintTrack;
}
Tells if the track (area the slider slides in) is to be painted. |
public boolean getSnapToTicks() {
return snapToTicks;
}
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. |
boolean getSnapToValue() {
return snapToValue;
}
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. |
public SliderUI getUI() {
return(SliderUI)ui;
}
Gets the UI object which implements the L&F for this component. |
public String getUIClassID() {
return uiClassID;
}
Returns the name of the L&F class that renders this component. |
public int getValue() {
return getModel().getValue();
}
Returns the slider's current value
from the {@code BoundedRangeModel}. |
public boolean getValueIsAdjusting() {
return getModel().getValueIsAdjusting();
}
Returns the {@code valueIsAdjusting} property from the model. For
details on how this is used, see the {@code setValueIsAdjusting}
documentation. |
public boolean imageUpdate(Image img,
int infoflags,
int x,
int y,
int w,
int h) {
if (!isShowing()) {
return false;
}
// Check that there is a label with such image
Enumeration elements = labelTable.elements();
while (elements.hasMoreElements()) {
Component component = (Component) elements.nextElement();
if (component instanceof JLabel) {
JLabel label = (JLabel) component;
if (SwingUtilities.doesIconReferenceImage(label.getIcon(), img) ||
SwingUtilities.doesIconReferenceImage(label.getDisabledIcon(), img)) {
return super.imageUpdate(img, infoflags, x, y, w, h);
}
}
}
return false;
}
|
protected String paramString() {
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;
}
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. |
public void removeChangeListener(ChangeListener l) {
listenerList.remove(ChangeListener.class, l);
}
Removes a ChangeListener from the slider. |
public void setExtent(int extent) {
getModel().setExtent(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. This method just
forwards the new extent value to the model.
The data model (an instance of {@code BoundedRangeModel})
handles any mathematical
issues arising from assigning faulty values. See the
{@code BoundedRangeModel} documentation for details.
If the new extent value is different from the previous extent value,
all change listeners are notified. |
public void setFont(Font font) {
super.setFont(font);
updateLabelSizes();
}
|
public void setInverted(boolean b) {
boolean oldValue = isInverted;
isInverted = b;
firePropertyChange("inverted", oldValue, isInverted);
if (b != oldValue) {
repaint();
}
}
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.
By default, the value of this property is {@code false}. |
public void setLabelTable(Dictionary labels) {
Dictionary oldTable = labelTable;
labelTable = labels;
updateLabelUIs();
firePropertyChange("labelTable", oldTable, labelTable );
if (labels != oldTable) {
revalidate();
repaint();
}
}
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 }.
An easy way to generate a standard table of value labels is by using the
{@code createStandardLabels} method.
Once the labels have been set, this method calls #updateLabelUIs .
Note that the labels are only painted if the {@code paintLabels}
property is {@code true}. |
public void setMajorTickSpacing(int n) {
int oldValue = majorTickSpacing;
majorTickSpacing = n;
if ( labelTable == null && getMajorTickSpacing() > 0 && getPaintLabels() ) {
setLabelTable( createStandardLabels( getMajorTickSpacing() ) );
}
firePropertyChange("majorTickSpacing", oldValue, majorTickSpacing);
if (majorTickSpacing != oldValue && getPaintTicks()) {
repaint();
}
}
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.
In order for major ticks to be painted, {@code setPaintTicks} must be
set to {@code true}.
This method will also set up a label table for you.
If there is not already a label table, and the major tick spacing is
{@code > 0}, and {@code getPaintLabels} returns
{@code true}, a standard label table will be generated (by calling
{@code createStandardLabels}) with labels at the major tick marks.
For the example above, you would get text labels: "0",
"10", "20", "30", "40", "50".
The label table is then set on the slider by calling
{@code setLabelTable}. |
public void setMaximum(int maximum) {
int oldMax = getModel().getMaximum();
getModel().setMaximum(maximum);
firePropertyChange( "maximum", Integer.valueOf( oldMax ), Integer.valueOf( maximum ) );
}
Sets the slider's maximum value to {@code maximum}. This method
forwards the new maximum value to the model.
The data model (an instance of {@code BoundedRangeModel})
handles any mathematical
issues arising from assigning faulty values. See the
{@code BoundedRangeModel} documentation for details.
If the new maximum value is different from the previous maximum value,
all change listeners are notified. |
public void setMinimum(int minimum) {
int oldMin = getModel().getMinimum();
getModel().setMinimum(minimum);
firePropertyChange( "minimum", Integer.valueOf( oldMin ), Integer.valueOf( minimum ) );
}
Sets the slider's minimum value to {@code minimum}. This method
forwards the new minimum value to the model.
The data model (an instance of {@code BoundedRangeModel})
handles any mathematical
issues arising from assigning faulty values. See the
{@code BoundedRangeModel} documentation for details.
If the new minimum value is different from the previous minimum value,
all change listeners are notified. |
public void setMinorTickSpacing(int n) {
int oldValue = minorTickSpacing;
minorTickSpacing = n;
firePropertyChange("minorTickSpacing", oldValue, minorTickSpacing);
if (minorTickSpacing != oldValue && getPaintTicks()) {
repaint();
}
}
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.
In order for minor ticks to be painted, {@code setPaintTicks} must be
set to {@code true}. |
public void setModel(BoundedRangeModel newModel) {
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 : Integer.valueOf(oldModel.getValue())),
(newModel == null
? null : Integer.valueOf(newModel.getValue())));
}
firePropertyChange("model", oldModel, sliderModel);
}
Sets the {@code BoundedRangeModel} that handles the slider's three
fundamental properties: minimum, maximum, value.
Attempts to pass a {@code null} model to this method result in
undefined behavior, and, most likely, exceptions. |
public void setOrientation(int orientation) {
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();
}
}
Set the slider's orientation to either {@code SwingConstants.VERTICAL} or
{@code SwingConstants.HORIZONTAL}. |
public void setPaintLabels(boolean b) {
boolean oldValue = paintLabels;
paintLabels = b;
if ( labelTable == null && getMajorTickSpacing() > 0 ) {
setLabelTable( createStandardLabels( getMajorTickSpacing() ) );
}
firePropertyChange("paintLabels", oldValue, paintLabels);
if (paintLabels != oldValue) {
revalidate();
repaint();
}
}
Determines whether labels are painted on the slider.
This method will also set up a label table for you.
If there is not already a label table, and the major tick spacing is
{@code > 0},
a standard label table will be generated (by calling
{@code createStandardLabels}) with labels at the major tick marks.
The label table is then set on the slider by calling
{@code setLabelTable}.
By default, this property is {@code false}. |
public void setPaintTicks(boolean b) {
boolean oldValue = paintTicks;
paintTicks = b;
firePropertyChange("paintTicks", oldValue, paintTicks);
if (paintTicks != oldValue) {
revalidate();
repaint();
}
}
Determines whether tick marks are painted on the slider.
By default, this property is {@code false}. |
public void setPaintTrack(boolean b) {
boolean oldValue = paintTrack;
paintTrack = b;
firePropertyChange("paintTrack", oldValue, paintTrack);
if (paintTrack != oldValue) {
repaint();
}
}
Determines whether the track is painted on the slider.
By default, this property is {@code true}. |
public void setSnapToTicks(boolean b) {
boolean oldValue = snapToTicks;
snapToTicks = b;
firePropertyChange("snapToTicks", oldValue, snapToTicks);
}
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.
By default, this property is {@code false}. |
void setSnapToValue(boolean b) {
boolean oldValue = snapToValue;
snapToValue = b;
firePropertyChange("snapToValue", oldValue, snapToValue);
}
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 {@code snapToTicks} property has also been
set to {@code true}, the snap-to-ticks behavior will prevail.
By default, the snapToValue property is {@code true}. |
public void setUI(SliderUI ui) {
super.setUI(ui);
}
Sets the UI object which implements the L&F for this component. |
public void setValue(int n) {
BoundedRangeModel m = getModel();
int oldValue = m.getValue();
if (oldValue == n) {
return;
}
m.setValue(n);
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
Integer.valueOf(oldValue),
Integer.valueOf(m.getValue()));
}
}
Sets the slider's current value to {@code n}. This method
forwards the new value to the model.
The data model (an instance of {@code BoundedRangeModel})
handles any mathematical
issues arising from assigning faulty values. See the
{@code BoundedRangeModel} documentation for details.
If the new value is different from the previous value,
all change listeners are notified. |
public void setValueIsAdjusting(boolean b) {
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));
}
}
Sets the model's {@code valueIsAdjusting} property. Slider look and
feel implementations should set this property to {@code true} when
a knob drag begins, and to {@code false} when the drag ends. |
protected void updateLabelUIs() {
Dictionary labelTable = getLabelTable();
if (labelTable == null) {
return;
}
Enumeration labels = labelTable.keys();
while ( labels.hasMoreElements() ) {
JComponent component = (JComponent) labelTable.get(labels.nextElement());
component.updateUI();
component.setSize(component.getPreferredSize());
}
}
Updates the UIs for the labels in the label table by calling
{@code updateUI} on each label. The UIs are updated from
the current look and feel. The labels are also set to their
preferred size. |
public void updateUI() {
setUI((SliderUI)UIManager.getUI(this));
// The labels preferred size may be derived from the font
// of the slider, so we must update the UI of the slider first, then
// that of labels. This way when setSize is called the right
// font is used.
updateLabelUIs();
}
Resets the UI property to a value from the current look and feel. |