public class Slider
ImplementsActionSource, Animation, Editable, IconHolder, StyleListener, TextHolder
Known subtypesSliderBridge
The slider component serves both as a slider widget to allow users to select a value on a scale via touch/arrows and also to indicate progress. The slider defaults to percentage display but can represent any positive set of values.
Slider is very versatile and can be used to represent things as diverse as the 5 star ranking UI
demonstrated below. Notice that for the UI to work correctly you need to enclose it in a layout that preserves
its preferred size like flow layout.
public void showForm() {
Form hi = new Form("Star Slider", new BoxLayout(BoxLayout.Y_AXIS));
hi.add(FlowLayout.encloseCenter(createStarRankSlider()));
hi.show();
}
private void initStarRankStyle(Style s, Image star) {
s.setBackgroundType(Style.BACKGROUND_IMAGE_TILE_BOTH);
s.setBorder(Border.createEmpty());
s.setBgImage(star);
s.setBgTransparency(0);
}
private Slider createStarRankSlider() {
Slider starRank = new Slider();
starRank.setEditable(true);
starRank.setMinValue(0);
starRank.setMaxValue(10);
Font fnt = Font.createTrueTypeFont("native:mainLight", "native:mainLight").
derive(Display.getInstance().convertToPixels(5, true), Font.STYLE_PLAIN);
Style s = new Style(0xffff33, 0, fnt, (byte)0);
Image fullStar = FontImage.createMaterial(FontImage.MATERIAL_STAR, s).toImage();
s.setOpacity(100);
s.setFgColor(0);
Image emptyStar = FontImage.createMaterial(FontImage.MATERIAL_STAR, s).toImage();
initStarRankStyle(starRank.getSliderEmptySelectedStyle(), emptyStar);
initStarRankStyle(starRank.getSliderEmptyUnselectedStyle(), emptyStar);
initStarRankStyle(starRank.getSliderFullSelectedStyle(), fullStar);
initStarRankStyle(starRank.getSliderFullUnselectedStyle(), fullStar);
starRank.setPreferredSize(new Dimension(fullStar.getWidth() * 5, fullStar.getHeight()));
return starRank;
}
Slider can be used as a progress indicator for network operations when combined with the
com.codename1.components.SliderBridge component:
Form hi = new Form("Download Progress", new BorderLayout());
Slider progress = new Slider();
Button download = new Button("Download");
download.addActionListener((e) -> {
ConnectionRequest cr = new ConnectionRequest("https://www.codenameone.com/img/blog/new_icon.png", false);
SliderBridge.bindProgress(cr, progress);
NetworkManager.getInstance().addToQueueAndWait(cr);
if(cr.getResponseCode() == 200) {
hi.add(BorderLayout.CENTER, new ScaleImageLabel(EncodedImage.create(cr.getResponseData())));
hi.revalidate();
}
});
hi.add(BorderLayout.SOUTH, progress).add(BorderLayout.NORTH, download);
hi.show();
Constructors
public Slider() | The default constructor uses internal rendering to draw its state |
Methods
public static Slider createInfinite() | Creates an infinite progress slider |
public final void setUIID(String id) | This method sets the Component the Unique identifier. |
protected boolean isStickyDrag() | Returns true if the component is interested in receiving drag/pointer release events even after the gesture exceeded its boundaries. |
public void initComponent() | Allows subclasses to bind functionality that relies on fully initialized and “ready for action” component state |
public void deinitialize() | Invoked to indicate that the component initialization is being reversed since the component was detached from the container hierarchy. |
public boolean animate() | Allows the animation to reduce “repaint” calls when it returns false. |
public boolean isInfinite() | The infinite slider functionality is used to animate progress for which there is no defined value. |
public void setInfinite(boolean i) | Activates/disables the infinite slider functionality used to animate progress for which there is no defined value. |
public void refreshTheme(boolean merge) | Makes sure the component is up to date with the current theme, ONLY INVOKE THIS METHOD IF YOU CHANGED THE THEME! |
public int getProgress() | Indicates the value of progress made |
public void setProgress(int value) | Indicates the value of progress made, this method is thread safe and can be invoked from any thread although discretion should still be kept so one thread doesn’t regress progress made by another thread… |
public int getProgress(ActionEvent evt) | Gets the progress of the slider at the point where the provided ActionEvent was triggered. |
protected void setProgressInternal(int value) | Internal slider-value updater used by drag/keyboard/setProgress paths. |
protected String formattedValue(int value) | Allows formatting the appearance of the progress when text is drawn on top |
public Style getSliderFullUnselectedStyle() | Returns the com.codename1.ui.plaf.Style used to paint the slider when its full |
public Style getSliderFullSelectedStyle() | Returns the com.codename1.ui.plaf.Style used to paint the slider when its full and selected |
public Style getSliderEmptyUnselectedStyle() | Returns the com.codename1.ui.plaf.Style used to paint the slider when its full |
public Style getSliderEmptySelectedStyle() | Returns the com.codename1.ui.plaf.Style used to paint the slider when its full and selected |
public Style getStyle() | Returns the current Component Style allowing code to draw the current component, you should normally use getUnselected/Pressed/DisabledStyle() and not this method since it will return different values based on component state. |
protected Dimension calcPreferredSize() | Return the size we would generally like for the component |
public void paintComponentBackground(Graphics g) | Paint the progress indicator |
public boolean isVertical() | Indicates the slider is vertical |
public void setVertical(boolean vertical) | Indicates the slider is vertical |
public boolean isEditable() | Indicates the slider is modifyable |
public void setEditable(boolean editable) | Indicates the slider is modifyable |
public void pointerPressed(int x, int y) | If this Component is focused, the pointer pressed event will call this method |
public void pointerDragged(int x, int y) | If this Component is focused, the pointer dragged event will call this method |
protected void fireClicked() | When working in 3 softbutton mode “fire” key (center softbutton) is sent to this method in order to allow 3 button devices to work properly. |
protected boolean isSelectableInteraction() | This method allows a component to indicate that it is interested in an “implicit” select command to appear in the “fire” button when 3 softbuttons are defined in a device. |
public void pointerReleased(int x, int y) | If this Component is focused, the pointer released event will call this method |
public void keyReleased(int code) | If this Component is focused, the key released event will call this method |
public void keyPressed(int code) | If this Component is focused, the key pressed event will call this method |
public int getIncrements() | The increments when the user presses a key to the left/right/up/down etc. |
public void setIncrements(int increments) | The increments when the user presses a key to the left/right/up/down etc. |
public void addDataChangedListener(DataChangedListener l) | Adds a listener to data changed events, notice that the status argument to the data change listener shouldn’t be relied upon. |
public void removeDataChangedListener(DataChangedListener l) | Removes a listener from data changed events, notice that the status argument to the data change listener shouldn’t be relied upon. |
public void addActionListener(ActionListener l) | Action listeners give a more coarse event only when the user lifts the finger from the slider |
public void removeActionListener(ActionListener l) | Action listeners give a more coarse event only when the user lifts the finger from the slider |
public boolean isRenderPercentageOnTop() | Indicates that the value of the slider should be rendered with a percentage sign on top of the slider. |
public void setRenderPercentageOnTop(boolean renderPercentageOnTop) | Indicates that the value of the slider should be rendered with a percentage sign on top of the slider. |
public boolean isRenderValueOnTop() | |
public void setRenderValueOnTop(boolean renderValueOnTop) | |
public int getMaxValue() | |
public void setMaxValue(int maxValue) | |
public int getMinValue() | |
public void setMinValue(int minValue) | |
public Image getThumbImage() | The thumb image is drawn on top of the current progress |
public void setThumbImage(Image thumbImage) | The thumb image is drawn on top of the current progress |
protected boolean shouldBlockSideSwipe() | A component that might need side swipe such as the slider could block it from being used for some other purpose when on top of said component. |
Inherited fields
From Component
DEFAULT_CURSOR, CROSSHAIR_CURSOR, TEXT_CURSOR, WAIT_CURSOR, SW_RESIZE_CURSOR, SE_RESIZE_CURSOR, NW_RESIZE_CURSOR, NE_RESIZE_CURSOR, N_RESIZE_CURSOR, S_RESIZE_CURSOR, W_RESIZE_CURSOR, E_RESIZE_CURSOR, HAND_CURSOR, MOVE_CURSOR, DRAG_REGION_NOT_DRAGGABLE, DRAG_REGION_POSSIBLE_DRAG_X, DRAG_REGION_POSSIBLE_DRAG_Y, DRAG_REGION_POSSIBLE_DRAG_XY, DRAG_REGION_LIKELY_DRAG_X, DRAG_REGION_LIKELY_DRAG_Y, DRAG_REGION_LIKELY_DRAG_XY, DRAG_REGION_IMMEDIATELY_DRAG_X, DRAG_REGION_IMMEDIATELY_DRAG_Y, DRAG_REGION_IMMEDIATELY_DRAG_XY, BRB_CONSTANT_ASCENT, BRB_CONSTANT_DESCENT, BRB_CENTER_OFFSET, BRB_OTHER, CENTER, TOP, LEFT, BOTTOM, RIGHT, BASELINE
Inherited methods
From Label
getDefaultGap, setDefaultGap, isDefaultTickerEnabled, setDefaultTickerEnabled, laidOut, getBadgeText, setBadgeText, setBadgeUIID, getBadgeStyleComponent, getIconStyleComponent, setFontIcon, setMaterialIcon, setFontIcon, getMaterialIcon, setMaterialIcon, getFontIcon, setFontIcon, getMaterialIconSize, getFontIconSize, getIconFont, getBaselineResizeBehavior, getBaseline, initLaf, getText, setText, getIcon, setIcon, getVerticalAlignment, setVerticalAlignment, getAlignment, setAlignment, getTextPosition, setTextPosition, getGap, setGap, paramString, paint, getMaxAutoSize, setMaxAutoSize, getMinAutoSize, setMinAutoSize, getShiftText, setShiftText, shouldTickerStart, startTicker, startTicker, stopTicker, isTickerRunning, isTickerEnabled, setTickerEnabled, isEndsWith3Points, setEndsWith3Points, getMask, setMask, getMaskName, setMaskName, getPropertyNames, getPropertyTypes, getPropertyTypeNames, getPropertyValue, setPropertyValue, getMaskedIcon, getBindablePropertyNames, getBindablePropertyTypes, bindProperty, unbindProperty, getBoundPropertyValue, setBoundPropertyValue, isShouldLocalize, setShouldLocalize, getShiftMillimeters, setShiftMillimeters, setShiftMillimeters, getShiftMillimetersF, isShowEvenIfBlank, setShowEvenIfBlank, getStringWidth, isLegacyRenderer, setLegacyRenderer, styleChanged, initUnselectedStyle, isAutoSizeMode, setAutoSizeMode, isTextSelectionEnabled, setTextSelectionEnabled, getTextSelectionSupport, getIconUIID, setIconUIID
From Component
setSameSize, isSetCursorSupported, parsePreferredSize, getDefaultDragTransparency, setDefaultDragTransparency, getEditingDelegate, setEditingDelegate, getCursor, setCursor, showNativeOverlay, hideNativeOverlay, updateNativeOverlay, getNativeOverlay, getAllStyles, getSameWidth, setSameWidth, getSameHeight, setSameHeight, getUIManager, getX, setX, getOuterX, getInnerX, getY, setY, getOuterY, getInnerY, isVisible, setVisible, getClientProperty, stripMarginAndPadding, clearClientProperties, putClientProperty, getDirtyRegion, setDirtyRegion, isOpaque, setOpaque, getWidth, setWidth, getOuterWidth, getInnerWidth, getHeight, setHeight, getOuterHeight, getInnerHeight, isDragRegion, getDragRegionStatus, getPreferredSizeStr, setPreferredSizeStr, getPreferredSize, setPreferredSize, getScrollDimension, calcScrollSize, setScrollSize, getPreferredW, setPreferredW, getPreferredH, setPreferredH, getOuterPreferredH, getInnerPreferredH, getOuterPreferredW, getInnerPreferredW, setSize, getUIID, setUIIDFinal, setUIID, getInlineAllStyles, setInlineAllStyles, getInlineSelectedStyles, setInlineSelectedStyles, getInlineUnselectedStyles, setInlineUnselectedStyles, getInlineDisabledStyles, setInlineDisabledStyles, getInlinePressedStyles, setInlinePressedStyles, remove, getParent, getOwner, setOwner, isOwnedBy, containsOrOwns, addFocusListener, removeFocusListener, addScrollListener, removeScrollListener, getSelectCommandText, setSelectCommandText, getLabelForComponent, setLabelForComponent, focusGained, focusLost, paintBackgrounds, paintShadows, getAbsoluteX, getAbsoluteY, isInClippingRegion, paintIntersectingComponentsAbove, paintScrollbars, paintScrollbarX, getScrollOpacity, getSelectedRect, paintScrollbarY, paintComponent, paintComponent, getBorder, getScrollable, paintBackground, isScrollable, isScrollableX, isScrollableY, getScrollX, setScrollX, getScrollY, setScrollY, onScrollX, onScrollY, getDraggedx, getDraggedy, getBottomGap, getSideGap, contains, visibleBoundsContains, hasFixedPreferredSize, getBounds, getBounds, getVisibleBounds, getVisibleBounds, isFocusable, setFocusable, onSetFocusable, resetFocusable, getTabIndex, setTabIndex, getPreferredTabIndex, setPreferredTabIndex, isTraversable, setTraversable, setShouldCalcPreferredSize, handlesInput, setHandlesInput, consumesRawTextInput, hasFocus, setFocus, getComponentForm, getTopLevelContainer, repaint, repaint, longKeyPress, keyRepeated, registerForAnimation, deregisterFromAnimation, getAnimationManager, getScrollAnimationSpeed, setScrollAnimationSpeed, isBlockLead, setBlockLead, isIgnorePointerEvents, setIgnorePointerEvents, isRippleEffect, setRippleEffect, getInlineStylesTheme, setInlineStylesTheme, shouldRenderComponentSelection, isHideInLandscape, setHideInLandscape, createStyleAnimation, isSmoothScrolling, setSmoothScrolling, pointerHover, stopScrollMomentum, pointerHoverReleased, pointerHoverPressed, pinch, pinchReleased, pinch, rotation, isPinchBlocksDragAndDrop, setPinchBlocksDragAndDrop, pointerDragged, getDragImage, getDragTransparency, setDragTransparency, toImage, dragInitiated, drawDraggedImage, draggingOver, dragEnter, dragExit, drop, addPullToRefresh, setPullToRefresh, respondsToPointerEvents, pointerPressed, isDragAndDropOperation, pointerReleased, longPointerPress, setVerticalScrollBounds, setHorizontalScrollBounds, isVScrollThumbGrabbed, isHScrollThumbGrabbed, isVScrollThumbHover, isHScrollThumbHover, isTensileDragEnabled, setTensileDragEnabled, addDropListener, removeDropListener, addDragOverListener, removeDragOverListener, isNativeDragSource, setNativeDragSource, getNativeDragOperation, setNativeDragOperation, createNativeDragOperation, isNativeDropTarget, setNativeDropTarget, getAcceptedDropMimeTypes, setAcceptedDropMimeTypes, getAcceptedDropActions, setAcceptedDropActions, canAcceptNativeDrop, nativeDragEnter, nativeDragOver, nativeDragExit, nativeDrop, addNativeDropListener, removeNativeDropListener, addNativeDragOverListener, removeNativeDragOverListener, dragFinished, addDragFinishedListener, addStateChangeListener, removeStateChangeListener, addPointerPressedListener, addLongPressListener, addContextMenuListener, removeContextMenuListener, addMouseWheelListener, removeMouseWheelListener, addStylusListener, removeStylusListener, mouseWheel, paintRippleOverlay, removePointerPressedListener, removeLongPressListener, removeDragFinishedListener, addPointerReleasedListener, removePointerReleasedListener, addPointerDraggedListener, removePointerDraggedListener, getDragSpeed, getPressedStyle, setPressedStyle, initPressedStyle, initDisabledStyle, initSelectedStyle, getUnselectedStyle, setUnselectedStyle, getSelectedStyle, setSelectedStyle, getDisabledStyle, setDisabledStyle, installDefaultPainter, requestFocus, toString, refreshTheme, refreshTheme, isDragActivated, getGridPosY, getGridPosX, scrollRectToVisible, scrollRectToVisible, paintBorder, paintBorderBackground, isCellRenderer, setCellRenderer, isScrollVisible, setScrollVisible, setIsScrollVisible, startEditingAsync, stopEditing, isEditing, isInitialized, setInitialized, getNextFocusDown, setNextFocusDown, getNextFocusUp, setNextFocusUp, getNextFocusLeft, setNextFocusLeft, getNextFocusRight, setNextFocusRight, isEnabled, setEnabled, getName, setName, initCustomStyle, deinitializeCustomStyle, isRTL, setRTL, isTactileTouch, isTactileTouch, setTactileTouch, paintLockRelease, paintLock, isSnapToGrid, setSnapToGrid, shouldBlockSideSwipeLeft, shouldBlockSideSwipeRight, blocksSideSwipe, isFlatten, setFlatten, getTensileLength, setTensileLength, isGrabsPointerEvents, setGrabsPointerEvents, getScrollOpacityChangeSpeed, setScrollOpacityChangeSpeed, growShrink, isAlwaysTensile, setAlwaysTensile, isDraggable, setDraggable, isDropTarget, setDropTarget, isChildOf, isHideInPortrait, setHideInPortrait, cancelRepaints, getCloudBoundProperty, setCloudBoundProperty, getCloudDestinationProperty, setCloudDestinationProperty, getComponentState, setComponentState, setHidden, isHidden, setHidden, isHidden, announceForAccessibility, getAccessibilityText, setAccessibilityText, getSemantics, getAccessibilityNode, accessibilityChanged, accessibilityChanged, getTooltip, setTooltip
Constructor details
Slider
public Slider()Method details
createInfinite
public static Slider createInfinite()Returns
setUIID
public final void setUIID(String id)Parameters
idString- UIID unique identifier for component type
isStickyDrag
protected boolean isStickyDrag()Returns
initComponent
public void initComponent()deinitialize
public void deinitialize()animate
public boolean animate()com.codename1.ui.Display class.Returns
isInfinite
public boolean isInfinite()Returns
setInfinite
public void setInfinite(boolean i)Parameters
iboolean- true for infinite progress
refreshTheme
public void refreshTheme(boolean merge)Parameters
mergeboolean- indicates if the current styles should be merged with the new styles
getProgress
public int getProgress()Returns
setProgress
public void setProgress(int value)Parameters
valueint- new value for progress
getProgress
public int getProgress(ActionEvent evt)Parameters
evtActionEvent- An ActionEvent that originated from this slider.
Returns
setProgressInternal
protected void setProgressInternal(int value)protected so subclasses (e.g. range-slider style multi-
thumb sliders) can route their own input through the same value-set +
repaint pipeline as the built-in handlers. See #1523.formattedValue
protected String formattedValue(int value)Parameters
valueint- the value of the slider
Returns
getSliderFullUnselectedStyle
public Style getSliderFullUnselectedStyle()com.codename1.ui.plaf.Style used to paint the slider when its fullReturns
getSliderFullSelectedStyle
public Style getSliderFullSelectedStyle()com.codename1.ui.plaf.Style used to paint the slider when its full and selectedReturns
getSliderEmptyUnselectedStyle
public Style getSliderEmptyUnselectedStyle()com.codename1.ui.plaf.Style used to paint the slider when its fullReturns
getSliderEmptySelectedStyle
public Style getSliderEmptySelectedStyle()com.codename1.ui.plaf.Style used to paint the slider when its full and selectedReturns
getStyle
public Style getStyle()Returns
calcPreferredSize
protected Dimension calcPreferredSize()Returns
paintComponentBackground
public void paintComponentBackground(Graphics g)Parameters
gGraphics- the component graphics
isVertical
public boolean isVertical()Returns
setVertical
public void setVertical(boolean vertical)Parameters
verticalboolean- true if the slider is vertical
isEditable
public boolean isEditable()Returns
setEditable
public void setEditable(boolean editable)Parameters
editableboolean- true if the slider is editable
pointerPressed
public void pointerPressed(int x, int y)Parameters
xint- the pointer x coordinate
yint- the pointer y coordinate
pointerDragged
public void pointerDragged(int x, int y)Parameters
xint- the pointer x coordinate
yint- the pointer y coordinate
fireClicked
protected void fireClicked()isSelectableInteraction
protected boolean isSelectableInteraction()Returns
pointerReleased
public void pointerReleased(int x, int y)Parameters
xint- the pointer x coordinate
yint- the pointer y coordinate
keyReleased
public void keyReleased(int code)Parameters
codeint- the key code value to indicate a physical key.
keyPressed
public void keyPressed(int code)Parameters
codeint- the key code value to indicate a physical key.
getIncrements
public int getIncrements()Returns
setIncrements
public void setIncrements(int increments)Parameters
incrementsint- increment value
addDataChangedListener
public void addDataChangedListener(DataChangedListener l)Parameters
lDataChangedListener- new listener
removeDataChangedListener
public void removeDataChangedListener(DataChangedListener l)Parameters
lDataChangedListener- listener to remove
addActionListener
public void addActionListener(ActionListener l)Parameters
lActionListener- the listener
removeActionListener
public void removeActionListener(ActionListener l)Parameters
lActionListener- the listener
isRenderPercentageOnTop
public boolean isRenderPercentageOnTop()Returns
setRenderPercentageOnTop
public void setRenderPercentageOnTop(boolean renderPercentageOnTop)Parameters
renderPercentageOnTopboolean- true to render percentages
isRenderValueOnTop
public boolean isRenderValueOnTop()Returns
setRenderValueOnTop
public void setRenderValueOnTop(boolean renderValueOnTop)Parameters
renderValueOnTopboolean- the renderValueOnTop to set
getMaxValue
public int getMaxValue()Returns
setMaxValue
public void setMaxValue(int maxValue)Parameters
maxValueint- the maxValue to set
getMinValue
public int getMinValue()Returns
setMinValue
public void setMinValue(int minValue)Parameters
minValueint- the minValue to set
getThumbImage
public Image getThumbImage()Returns
setThumbImage
public void setThumbImage(Image thumbImage)Parameters
thumbImageImage- the thumbImage to set
shouldBlockSideSwipe
protected boolean shouldBlockSideSwipe()