Class MultiThumbSlider<T>
- Type Parameters:
T- the type of data each float maps to. For example: in the GradientSlider this value is a Color. Sometimes this property may be unnecessary. If this slider is only meant to store the relative position of thumbs, then you may set this to a trivial stub-like object like a String or Character.
- All Implemented Interfaces:
ImageObserver,MenuContainer,Serializable
JSlider, except there are at least two thumbs. A JSlider is designed to
modify one number within a certain range of values. By contrast a MultiThumbSlider actually modifies a
table of data. Each thumb in a MultiThumbSlider should be thought of as a key, and it maps to an abstract
value. In the case of the GradientSlider: each value is a java.awt.Color. Other subclasses could
come along that map to other abstract objects. (For example, a VolumeSlider might map each thumb to a specific
volume level. This type of widget would let the user control fading in/out of an audio track.)
The slider graphically represents the domain from zero to one, so each thumb is always positioned within that domain. If the user drags a thumb outside this domain: that thumb disappears.
There is always a selected thumb in each slider when this slider has the keyboard focus. The user can press the tab key (or shift-tab) to transfer focus to different thumbs. Also the arrow keys can be used to control the selected thumb.
The user can click and drag any thumb to a new location. If a thumb is dragged so it is less than zero or greater than one:
then that thumb is removed. If the user clicks between two existing thumbs: a new thumb is created if autoAdd is
set to true. (If autoAdd is set to false: nothing happens.)
There are unimplemented methods in this class: doDoubleClick() and doPopup(). The UI will invoke
these methods as needed; this gives the user a chance to edit the values represented at a particular point.
Also using the keyboard:
- In a horizontal slider, the user can press modifier+left or modifer+right to insert a new thumb to the left/right of the
currently selected thumb. (Where "modifier" refers to
Toolkit.getDefaultTookit().getMenuShortcutKeyMask(). On Mac this is META, and on Windows this is CONTROL.) Likewise on a vertical slider the up/down arrow keys can be used to add thumbs. - The delete/backspace key can be used to remove thumbs.
- In a horizontal slider, the down arrow key can be used to invoke
doPopup(). This should invoke aJPopupMenuthat is keyboard accessible, so the user should be able to navigate this component without a mouse. Likewise on a vertical slider the right arrow key should do the same. - The space bar or return key invokes
doDoubleClick().
Because thumbs can be abstractly inserted, the values each thumb represents should be tween-able. That is, if there is a
value at zero and a value at one, the call getValue(.5f) must return a value that is halfway between those
values.
Also note that although the thumbs must always be between zero and one: the minimum and maximum thumbs do not have to be zero and one. The user can adjust them so the minimum thumb is, say, .2f, and the maximum thumb is .5f.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumA set of possible behaviors when one thumb collides with another.Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponentNested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainerNested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe property that is changed whensetValueIsAdjusting(b)is called.static final StringThe property that controls whether clicking between thumbs automatically adds a thumb.(package private) List<ChangeListener>ChangeListeners registered with this slider.static final StringThe property that is changed whensetCollisionPolicy(c)is called.static final intThe orientation constant for a horizontal slider.static final StringThe property that is changed whensetInverted(b)is called.static final StringThe property that is changed whensetOrientation(i)is called.static final StringThe property that is changed whensetPaintTicks(b)is called.static final StringThe property that controls whether the user can remove a thumb (either by dragging or with the delete key).static final StringThe property that is changed whensetSelectedThumb()is called.static final StringThe property that is changed whensetMinimumThumbnailCount(b)is called.static final StringThe property that is changed whensetInverted(b)is called.protected float[]The positions of the thumbsprotected T[]The values for each thumbstatic final StringThe property that is changed whensetValues()is called.static final intThe orientation constant for a vertical slider.Fields inherited from class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOWFields inherited from class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTFields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH -
Constructor Summary
ConstructorsConstructorDescriptionMultiThumbSlider(float[] thumbPositions, T[] values) Creates a new horizontal MultiThumbSlider.MultiThumbSlider(int orientation, float[] thumbPositions, T[] values) Creates a new MultiThumbSlider. -
Method Summary
Modifier and TypeMethodDescriptionvoidThis listener will be notified when the colors/positions of this slider are modified.intaddThumb(float pos) This creats and inserts a thumb at a position indicated.createValueForInsertion(float pos) This creates a new value for insertion.booleandoDoubleClick(int x, int y) An optional method subclasses can override to react to the user's double-click.booleandoPopup(int x, int y) An optional method subclasses can override to react to the user's request for a contextual menu.protected voidInvokes all the ChangeListeners.intintThe orientation of this slider.intReturns the selected thumb index, or -1 if this component doesn't have the keyboard focus.intgetSelectedThumb(boolean ignoreIfUnfocused) Returns the currently selected thumb index.intThe number of thumbs in this slider.float[]The thumb positions for this slider.getUI()T[]The values for thumbs for this slider.booleanbooleanbooleanTells if tick marks are to be painted.booleanbooleanbooleantrueif the user is current modifying this component.voidRemoves aChangeListenerfrom this slider.voidremoveThumb(int thumbIndex) Removes a specific thumbvoidsetAutoAdding(boolean b) Controls whether thumbs are automatically added when the user clicks in a space that doesn't already have a thumb.voidvoidsetInverted(boolean b) Assigns whether this slider is inverted or not.voidsetMinimumThumbnailCount(int i) voidsetOrientation(int i) Reassign the orientation of this slider.voidsetPaintTicks(boolean b) Turns on/off the painted tick marks for this slider.voidsetSelectedThumb(int index) Assigns the currently selected thumb.voidsetThumbOverlap(boolean i) voidsetThumbRemovalAllowed(boolean b) voidsetUI(MultiThumbSliderUi<T> ui) voidsetValueIsAdjusting(boolean b) This is used to notify other objects when the user is in the process of adjusting values in this slider.voidThis assigns new positions/values for the thumbs in this slider.voidDepending on which thumb is selected, this may shift the focus to the next available thumb, or it may shift the focus to the next focusableJComponent.voidDepending on which thumb is selected, this may shift the focus to the previous available thumb, or it may shift the focus to the previous focusableJComponent.voidupdateUI()Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, updateMethods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTreeMethods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocusUpCycle
-
Field Details
-
AUTOADD_PROPERTY
The property that controls whether clicking between thumbs automatically adds a thumb. -
REMOVAL_ALLOWED
The property that controls whether the user can remove a thumb (either by dragging or with the delete key). -
SELECTED_THUMB_PROPERTY
The property that is changed whensetSelectedThumb()is called. -
COLLISION_PROPERTY
The property that is changed whensetCollisionPolicy(c)is called. -
INVERTED_PROPERTY
The property that is changed whensetInverted(b)is called. -
THUMB_OVERLAP_PROPERTY
The property that is changed whensetInverted(b)is called. -
THUMB_MINIMUM_PROPERTY
The property that is changed whensetMinimumThumbnailCount(b)is called. -
ORIENTATION_PROPERTY
The property that is changed whensetOrientation(i)is called. -
VALUES_PROPERTY
The property that is changed whensetValues()is called. Note this is used when either the positions or the values are updated, because they need to be updated at the same time to maintain an exact one-to-one ratio. -
ADJUST_PROPERTY
The property that is changed whensetValueIsAdjusting(b)is called. -
PAINT_TICKS_PROPERTY
The property that is changed whensetPaintTicks(b)is called. -
thumbPositions
protected float[] thumbPositionsThe positions of the thumbs -
values
The values for each thumb -
changeListeners
List<ChangeListener> changeListenersChangeListeners registered with this slider. -
HORIZONTAL
public static final int HORIZONTALThe orientation constant for a horizontal slider.- See Also:
-
VERTICAL
public static final int VERTICALThe orientation constant for a vertical slider.- See Also:
-
-
Constructor Details
-
MultiThumbSlider
Creates a new horizontal MultiThumbSlider.- Parameters:
thumbPositions- an array of values from zero to one.values- an array of values, each value corresponds to a value inthumbPositions.
-
MultiThumbSlider
Creates a new MultiThumbSlider.- Parameters:
orientation- must beHORIZONTALorVERTICALthumbPositions- an array of values from zero to one.values- an array of values, each value corresponds to a value inthumbPositions.
-
-
Method Details
-
getUI
- Overrides:
getUIin classJComponent
-
updateUI
public void updateUI()- Overrides:
updateUIin classJComponent
-
setUI
- Parameters:
ui- slider
-
addChangeListener
This listener will be notified when the colors/positions of this slider are modified.Note you can also listen to these events by listening to the
VALUES_PROPERTY, but this mechanism is provided as a convenience to resemble theJSlidermodel.- Parameters:
l- theChangeListenerto add.
-
removeChangeListener
Removes aChangeListenerfrom this slider.- Parameters:
l- the listener to remove
-
fireChangeListeners
protected void fireChangeListeners()Invokes all the ChangeListeners. -
transferFocus
public void transferFocus()Depending on which thumb is selected, this may shift the focus to the next available thumb, or it may shift the focus to the next focusableJComponent.- Overrides:
transferFocusin classComponent
-
transferFocusBackward
public void transferFocusBackward()Depending on which thumb is selected, this may shift the focus to the previous available thumb, or it may shift the focus to the previous focusableJComponent.- Overrides:
transferFocusBackwardin classComponent
-
createValueForInsertion
This creates a new value for insertion.If the
posargument is outside the domain of thumbs, then a value still needs to be returned.- Parameters:
pos- a position between zero and one- Returns:
- a value that corresponds to the position
pos
-
removeThumb
public void removeThumb(int thumbIndex) Removes a specific thumb- Parameters:
thumbIndex- the thumb index to remove.
-
doDoubleClick
public boolean doDoubleClick(int x, int y) An optional method subclasses can override to react to the user's double-click. When a thumb is double-clicked the user is trying to edit the value for that thumb. A double-click probably suggests the user wants a detailed set of controls to edit a value, such as a dialog.Note this method will be called with arguments (-1,-1) if the space bar or return key is pressed.
By default this method does nothing, and returns
falseNote the (x,y) information passed to this method is only provided so subclasses can position components (such as a JPopupMenu). It can be assumed for a double-click event that the user has selected a thumb (since one click will click/create a thumb) and intends to edit the currently selected thumb.
- Parameters:
x- the x-value of the mouse click locationy- the y-value of the mouse click location- Returns:
trueif this event was consumed, or acted upon.falseif this is unimplemented.
-
doPopup
public boolean doPopup(int x, int y) An optional method subclasses can override to react to the user's request for a contextual menu. When a thumb is right-clicked the user is trying to edit the value for that thumb. A right-click probably suggests the user wants very quick, simple options to adjust a thumb.By default this method does nothing, and returns
false- Parameters:
x- the x-value of the mouse click locationy- the y-value of the mouse click location- Returns:
trueif this event was consumed, or acted upon.falseif this is unimplemented.
-
isPaintTicks
public boolean isPaintTicks()Tells if tick marks are to be painted.- Returns:
- whether ticks should be painted on this slider.
-
setPaintTicks
public void setPaintTicks(boolean b) Turns on/off the painted tick marks for this slider.This triggers a
PropertyChangeEventforPAINT_TICKS_PROPERTY.- Parameters:
b- whether tick marks should be painted
-
addThumb
public int addThumb(float pos) This creats and inserts a thumb at a position indicated.This method relies on the abstract
getValue(float)to determine what value to put at the new thumb location.- Parameters:
pos- the new thumb position- Returns:
- the index of the newly created thumb
-
setValueIsAdjusting
public void setValueIsAdjusting(boolean b) This is used to notify other objects when the user is in the process of adjusting values in this slider.A listener may not want to act on certain changes until this property is
falseif it is expensive to process certain changes.This triggers a
PropertyChangeEventforADJUST_PROPERTY.- Parameters:
b- new value
-
isValueAdjusting
public boolean isValueAdjusting()trueif the user is current modifying this component.- Returns:
- the value of the
adjustingproperty
-
getThumbPositions
public float[] getThumbPositions()The thumb positions for this slider.There is a one-to-one correspondence between this array and the
getValues()array.This array is always sorted in ascending order.
- Returns:
- an array of the positions of thumbs.
-
getValues
The values for thumbs for this slider.There is a one-to-one correspondence between this array and the
getThumbPositions()array.- Returns:
- an array of the values associated with each thumb.
-
setValues
This assigns new positions/values for the thumbs in this slider. The two must be assigned at exactly the same time, so there is always the same number of thumbs/sliders.This triggers a
PropertyChangeEventforVALUES_PROPERTY, and possibly for theSELECTED_THUMB_PROPERTYif that had to be adjusted, too.- Parameters:
thumbPositions- an array of the new position of each thumbvalues- an array of the value associated with each thumb- Throws:
IllegalArgumentException- if the size of the arrays are different, or if the thumbPositions array is not sorted in ascending order.
-
getThumbCount
public int getThumbCount()The number of thumbs in this slider.- Returns:
- the number of thumbs.
-
setSelectedThumb
public void setSelectedThumb(int index) Assigns the currently selected thumb. A value of -1 indicates that no thumb is currently selected.A slider should always have a selected thumb if it has the keyboard focus, though, so be careful when you modify this.
This triggers a
PropertyChangeEventforSELECTED_THUMB_PROPERTY.- Parameters:
index- the new selected thumb
-
getSelectedThumb
public int getSelectedThumb()Returns the selected thumb index, or -1 if this component doesn't have the keyboard focus.- Returns:
- the selected thumb index
-
getSelectedThumb
public int getSelectedThumb(boolean ignoreIfUnfocused) Returns the currently selected thumb index.Note this might be -1, indicating that there is no selected thumb.
It is recommend you use the
getSelectedThumb()method most of the time. This method is made public so UI's can provide a better user experience as this component gains and loses focus.- Parameters:
ignoreIfUnfocused- if this component doesn't have focus and this istrue, then this returns -1. If this isfalsethen this returns the internal value used to store the selected index, but the user may not realize this thumb is "selected".- Returns:
- the selected thumb
-
setAutoAdding
public void setAutoAdding(boolean b) Controls whether thumbs are automatically added when the user clicks in a space that doesn't already have a thumb.- Parameters:
b- whether auto adding is active or not
-
isAutoAdding
public boolean isAutoAdding()- Returns:
- whether thumbs are automatically added when the user clicks in a space that doesn't already have a thumb.
-
getOrientation
public int getOrientation()The orientation of this slider.- Returns:
- HORIZONTAL or VERTICAL
-
setOrientation
public void setOrientation(int i) Reassign the orientation of this slider.- Parameters:
i- must be HORIZONTAL or VERTICAL
-
isInverted
public boolean isInverted()- Returns:
- whether this slider is inverted or not.
-
setInverted
public void setInverted(boolean b) Assigns whether this slider is inverted or not.- Parameters:
b- inverted slider or not This triggers aPropertyChangeEventforINVERTED_PROPERTY.
-
getCollisionPolicy
-
setCollisionPolicy
-
isThumbRemovalAllowed
public boolean isThumbRemovalAllowed() -
setThumbRemovalAllowed
public void setThumbRemovalAllowed(boolean b) -
setMinimumThumbnailCount
public void setMinimumThumbnailCount(int i) -
getMinimumThumbnailCount
public int getMinimumThumbnailCount() -
setThumbOverlap
public void setThumbOverlap(boolean i) -
isThumbOverlap
public boolean isThumbOverlap()
-