public class Sheet
ImplementsAnimation, Editable, Iterable<Component>, StyleListener
A light-weight dialog that slides up from the bottom of the screen on mobile devices.
Sheets include a “title” bar, with a back/close button, a title label, and a “commands container” (#getCommandsContainer())
which allows you to insert your own custom components (usually buttons) in the upper right.
Custom content should be placed inside the content pane which can be retrieved via #getContentPane()
Usage
The general usage is to create new Sheet instance (or subclass), then call #show()
to make it appear over the current form. If a different sheet that is currently being displayed, then
calling #show() will replace it.
Inter-Sheet Navigation
The java.lang.String) constructor can take another
Sheet object as a parameter, which will act as a “parent” sheet (#getParentSheet(). If the parent
sheet is not null, then this sheet will have a “Back” button instead of a “Close” button. THe
“Back” button will navigate back to the parent sheet.
When navigating between sheets, the sheet will be resized with a smooth slide animation to the preferred height of the destination sheet.
Example
`public void start() {
if(current != null){
current.show();
return;`
Form hi = new Form("Hi World", new BorderLayout());
Button b = new Button("Open Sheet");
b.addActionListener(e->{
new MySheet(null).show();
});
hi.add(BorderLayout.NORTH, b);
hi.show();
}
private class MySheet extends Sheet {
MySheet(Sheet parent) {
super(parent, "My Sheet");
Container cnt = getContentPane();
cnt.setLayout(BoxLayout.y());
Button gotoSheet2 = new Button("Goto Sheet 2");
gotoSheet2.addActionListener(e->{
new MySheet2(this).show(300);
});
cnt.add(gotoSheet2);
for (String t : new String[]{"Red", "Green", "Blue", "Orange"}) {
cnt.add(new Label(t));
}
}
}
private class MySheet2 extends Sheet {
MySheet2(Sheet parent) {
super(parent, "Sheet 2");
Container cnt = getContentPane();
cnt.setLayout(BoxLayout.y());
cnt.setScrollableY(true);
for (int i=0; iVideo Sample
[Screen cast of the SheetSample demo](https://youtu.be/3okEj_JW3-k)
View source for this sample [here](https://github.com/codenameone/CodenameOne/tree/master/Samples/samples/SheetSample).
This sample can be run directly in the [SampleRunner](https://github.com/codenameone/CodenameOne/tree/master/Samples/).
@author shannah
Constructors
public Sheet(Sheet parent, String title) | Creates a new sheet with the specified parent and title. |
public Sheet(Sheet parent, String title, String uiid) | Creates a new sheet with the specified parent and title. |
Methods
public static boolean isSheetVisibleAt(int x, int y) | |
public void setTopLevelHost(TopLevelContainer host) | Sets the top level this sheet appears on, which may be a com.codename1.ui.Window rather than the current Form. |
public TopLevelContainer getTopLevelHost() | Returns the top level set with #setTopLevelHost(TopLevelContainer). |
public static Sheet getCurrentSheet() | Gets the current sheet on the current form or null if no sheet is currently being displayed. |
public static Sheet getCurrentSheet(TopLevelContainer top) | Gets the sheet currently showing on the given top level, or null. |
public static Sheet findContainingSheet(Component cmp) | Finds Sheet containing this component if it is currently part of a Sheet. |
public boolean isAllowClose() | Checks whether the user is allowed to close this sheet. |
public void setAllowClose(boolean allowClose) | Sets whether the user is able to close this sheet. |
public boolean isSwipeToDismissEnabled() | Checks whether this sheet can be dismissed by swiping it toward the edge of the screen (e.g. swiping down for a south-positioned sheet). |
public void setSwipeToDismissEnabled(boolean swipeToDismissEnabled) | Enables or disables the swipe-to-dismiss gesture. |
public Container getContentPane() | Gets the content pane of the sheet. |
public void hideBackButton() | Hides the back button. |
public void showBackButton() | Shows the back button. |
public Container getCommandsContainer() | Gets the container that is rendered on the top right bar of the sheet. |
public String getTitle() | Gets the title text displayed in the default title label. |
public void setTitle(String title) | Sets the title text displayed in the default title label. |
public Component getTitleComponent() | Gets the component currently used in the center of the title bar. |
public void setTitleComponent(Component cmp) | Sets the title component rendered in the center of the title bar. |
public void show() | Shows the sheet with the default (300ms) transition duration. |
public <T> AsyncResource<T> showForResult() | Shows the sheet and returns an AsyncResource that will be completed when the sheet finishes – either with #finish(Object) carrying a chosen value, or with null when the sheet is dismissed via back/swipe. |
public <T> AsyncResource<T> showForResult(int duration) | #showForResult with a custom slide duration. |
public void finish(Object result) | Completes the result resource returned by #showForResult and dismisses the sheet. |
public void show(int duration) | Shows the sheet over the current form using a slide-up transition with given duration in milliseconds. |
public String getPosition() | Gets the position where the Sheet is to be displayed. |
public void setPosition(String position) | Sets the position where the Sheet is to be displayed. |
public void setPosition(String phonePosition, String tabletPosition) | Sets the position where the Sheet is to be displayed. |
public void back() | Goes back to the parent sheet with a default (300ms) slide animation. |
public void back(int duration) | Goes back to the parent sheet with a slide animation of given duration. |
public void setX(int x) | Sets the Component x location relative to the parent container, this method is exposed for the purpose of external layout managers and should not be invoked directly. |
public void setY(int y) | Sets the Component y location relative to the parent container, this method is exposed for the purpose of external layout managers and should not be invoked directly. |
public void setWidth(int width) | Sets the Component width, this method is exposed for the purpose of external layout managers and should not be invoked directly. |
public void setHeight(int height) | Sets the Component height, this method is exposed for the purpose of external layout managers and should not be invoked directly. |
public Sheet getParentSheet() | Gets the parent sheet or null if there is none. |
protected void initComponent() | Allows subclasses to bind functionality that relies on fully initialized and “ready for action” component state |
protected void deinitialize() | Invoked to indicate that the component initialization is being reversed since the component was detached from the container hierarchy. |
public boolean isAncestorSheetOf(Sheet sheet) | Checks if the current sheet is an ancestor sheet of the given sheet. |
public void addCloseListener(ActionListener l) | Adds listener notified when the sheet is closed. |
public void removeCloseListener(ActionListener l) | Removes a close listener. |
public void addBackListener(ActionListener l) | Adds listener to be notified when user goes back to the parent. |
public void removeBackListener(ActionListener l) | Removes a back listener. |
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 Container
encloseIn, encloseIn, initLaf, getUIManager, setUIManager, isSurface, add, addAll, add, add, add, add, add, getLeadComponent, setLeadComponent, getLeadParent, keyPressed, keyReleased, getLayout, setLayout, invalidate, setShouldLayout, setShouldCalcPreferredSize, getLayoutWidth, getLayoutHeight, applyRTL, constrainWidthWhenScrollable, constrainHeightWhenScrollable, addComponent, addComponent, addComponent, addComponent, replaceAndWait, replaceAndWait, replace, replaceAndWait, replace, createReplaceTransition, isEnabled, setEnabled, removeComponent, cancelRepaints, flushReplace, removeAll, revalidateWithAnimationSafety, revalidate, revalidateLater, forceRevalidate, clearClientProperties, paint, paintGlass, layoutContainer, isSafeArea, setSafeArea, isSafeAreaRoot, getSafeAreaRoot, setSafeAreaRoot, getComponentCount, getComponentAt, getComponentIndex, contains, scrollComponentToVisible, getClosestComponentTo, getResponderAt, getComponentAt, findDropTargetAt, pointerPressed, calcPreferredSize, paramString, refreshTheme, isScrollableX, setScrollableX, isScrollableY, setScrollableY, getSideGap, getBottomGap, setScrollable, setCellRenderer, getScrollIncrement, setScrollIncrement, findFirstFocusable, dragInitiated, fireClicked, isSelectableInteraction, getGridPosY, paintComponentBackground, getGridPosX, animateHierarchyAndWait, createAnimateHierarchy, animateHierarchy, animateHierarchyFadeAndWait, createAnimateHierarchyFade, animateHierarchyFade, animateLayoutFadeAndWait, createAnimateLayoutFadeAndWait, animateLayoutFade, createAnimateLayoutFade, animateLayoutAndWait, animateLayout, updateTabIndices, createAnimateLayout, drop, createAnimateMotion, morph, morphAndWait, animateUnlayout, animateUnlayoutAndWait, createAnimateUnlayout, getChildrenAsList, iterator, iterator
From Component
setSameSize, isSetCursorSupported, parsePreferredSize, getDefaultDragTransparency, setDefaultDragTransparency, getEditingDelegate, setEditingDelegate, getCursor, setCursor, showNativeOverlay, hideNativeOverlay, updateNativeOverlay, getNativeOverlay, getAllStyles, getSameWidth, setSameWidth, getSameHeight, setSameHeight, getX, getOuterX, getInnerX, getY, getOuterY, getInnerY, isVisible, setVisible, getClientProperty, stripMarginAndPadding, putClientProperty, getDirtyRegion, setDirtyRegion, isOpaque, setOpaque, getWidth, getOuterWidth, getInnerWidth, getHeight, getOuterHeight, getInnerHeight, isDragRegion, getDragRegionStatus, getBaseline, getBaselineResizeBehavior, getPreferredSizeStr, setPreferredSizeStr, getPreferredSize, setPreferredSize, getScrollDimension, calcScrollSize, setScrollSize, getPreferredW, setPreferredW, getPreferredH, setPreferredH, getOuterPreferredH, getInnerPreferredH, getOuterPreferredW, getInnerPreferredW, setSize, getUIID, setUIID, 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, getScrollX, setScrollX, getScrollY, setScrollY, onScrollX, onScrollY, getDraggedx, getDraggedy, contains, visibleBoundsContains, hasFixedPreferredSize, getBounds, getBounds, getVisibleBounds, getVisibleBounds, isFocusable, setFocusable, onSetFocusable, resetFocusable, getTabIndex, setTabIndex, getPreferredTabIndex, setPreferredTabIndex, isTraversable, setTraversable, 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, drawDraggedImage, draggingOver, dragEnter, dragExit, addPullToRefresh, setPullToRefresh, respondsToPointerEvents, pointerDragged, isStickyDrag, pointerPressed, isDragAndDropOperation, pointerReleased, longPointerPress, pointerReleased, setVerticalScrollBounds, setHorizontalScrollBounds, isVScrollThumbGrabbed, isHScrollThumbGrabbed, isVScrollThumbHover, isHScrollThumbHover, isTensileDragEnabled, setTensileDragEnabled, getTextSelectionSupport, 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, getStyle, getPressedStyle, setPressedStyle, initUnselectedStyle, initPressedStyle, initDisabledStyle, initSelectedStyle, getUnselectedStyle, setUnselectedStyle, getSelectedStyle, setSelectedStyle, getDisabledStyle, setDisabledStyle, installDefaultPainter, requestFocus, toString, refreshTheme, refreshTheme, isDragActivated, animate, scrollRectToVisible, scrollRectToVisible, paintBorder, paintBorderBackground, isCellRenderer, isScrollVisible, setScrollVisible, setIsScrollVisible, startEditingAsync, stopEditing, isEditing, isEditable, laidOut, isInitialized, setInitialized, styleChanged, getNextFocusDown, setNextFocusDown, getNextFocusUp, setNextFocusUp, getNextFocusLeft, setNextFocusLeft, getNextFocusRight, setNextFocusRight, getName, setName, initCustomStyle, deinitializeCustomStyle, isRTL, setRTL, isTactileTouch, isTactileTouch, setTactileTouch, getPropertyNames, getPropertyTypes, getPropertyTypeNames, getPropertyValue, setPropertyValue, paintLockRelease, paintLock, isSnapToGrid, setSnapToGrid, shouldBlockSideSwipe, shouldBlockSideSwipeLeft, shouldBlockSideSwipeRight, blocksSideSwipe, isFlatten, setFlatten, getTensileLength, setTensileLength, isGrabsPointerEvents, setGrabsPointerEvents, getScrollOpacityChangeSpeed, setScrollOpacityChangeSpeed, growShrink, isAlwaysTensile, setAlwaysTensile, isDraggable, setDraggable, isDropTarget, setDropTarget, isChildOf, isHideInPortrait, setHideInPortrait, getBindablePropertyNames, getBindablePropertyTypes, bindProperty, unbindProperty, getBoundPropertyValue, setBoundPropertyValue, getCloudBoundProperty, setCloudBoundProperty, getCloudDestinationProperty, setCloudDestinationProperty, getComponentState, setComponentState, setHidden, isHidden, setHidden, isHidden, announceForAccessibility, getAccessibilityText, setAccessibilityText, getSemantics, getAccessibilityNode, accessibilityChanged, accessibilityChanged, getTooltip, setTooltip
Constructor details
Sheet
public Sheet(Sheet parent, String title)Parameters
parentSheet- Optional parent sheet. If non-null, then this sheet will have a “back” button instead of a “close” button. The “back” button will return to the parent sheet.
titleString- The title to display in the title bar of the sheet.
Sheet
public Sheet(Sheet parent, String title, String uiid)Parameters
parentSheet- Optional parent sheet. If non-null, then this sheet will have a “back” button instead of a “close” button. The “back” button will return to the parent sheet.
titleString- The title to display in the title bar of the sheet.
uiidString- Optional UIID for the sheet. If non-null, then the Sheet’s uiid will be uiid, the title label’s UIID will be uiid + “Title”, the title bar’s UIID will be uiid + “TitleBar”, and the back/close button’s UIID will be uiid + “BackButton”.
Method details
isSheetVisibleAt
public static boolean isSheetVisibleAt(int x, int y)setTopLevelHost
public void setTopLevelHost(TopLevelContainer host)com.codename1.ui.Window rather than the current Form.Parameters
hostTopLevelContainer- the top level to show on, or null to work it out
getTopLevelHost
public TopLevelContainer getTopLevelHost()#setTopLevelHost(TopLevelContainer).Returns
getCurrentSheet
public static Sheet getCurrentSheet()Returns
getCurrentSheet
public static Sheet getCurrentSheet(TopLevelContainer top)Parameters
topTopLevelContainer- the top level to look on, may be null
Returns
findContainingSheet
public static Sheet findContainingSheet(Component cmp)Parameters
cmpComponent- The component to check.
Returns
isAllowClose
public boolean isAllowClose()Returns
setAllowClose
public void setAllowClose(boolean allowClose)Sets whether the user is able to close this sheet. Default is true. If you set this value to false, then there will be no close button, and pressing outside of the sheet will have no effect.
Child sheets will assume the settings of the parent. The back button will still work, but the top level sheet will not include a close button.
Parameters
allowCloseboolean- True to allow user to close the sheet. False to prevent it.
isSwipeToDismissEnabled
public boolean isSwipeToDismissEnabled()Returns
setSwipeToDismissEnabled
public void setSwipeToDismissEnabled(boolean swipeToDismissEnabled)isAllowClose(); if allowClose is false the gesture is disabled
regardless of this flag.Parameters
swipeToDismissEnabledboolean- True to enable the swipe-to-dismiss gesture, false to disable it.
getContentPane
public Container getContentPane()Returns
hideBackButton
public void hideBackButton()showBackButton
public void showBackButton()getCommandsContainer
public Container getCommandsContainer()getTitle
public String getTitle()Returns
setTitle
public void setTitle(String title)Sets the title text displayed in the default title label.
If a custom title component is currently installed via setTitleComponent(Component),
this method still updates the default title label so that it will be shown if the title
component is reset back to null.
Parameters
titleString- The title text.
getTitleComponent
public Component getTitleComponent()Returns
setTitleComponent
public void setTitleComponent(Component cmp)Sets the title component rendered in the center of the title bar.
This allows for custom title layouts such as including an image above the title text.
If null is passed, the default title label is restored.
Parameters
cmpComponent- The component to use for the title area, or
nullto restore the default title label.
show
public void show()See also
showForResult
public <T> AsyncResource<T> showForResult()Shows the sheet and returns an AsyncResource that will be completed when
the sheet finishes – either with #finish(Object) carrying a chosen value,
or with null when the sheet is dismissed via back/swipe.
Lets sheets be used as inline confirmation dialogs / pickers without
wiring up addCloseListener + state-shared variables:
PickerSheet sheet = new PickerSheet();
sheet.<String>showForResult().ready(new SuccessCallback<String>() {
public void onSuccess(String picked) {
if (picked != null) handle(picked);
}
});
The result type is supplied at the call site; use Sheet#finish(Object)
internally to complete it. The cast is unchecked at runtime – pick a type
you control inside the sheet.
Since 7.0
showForResult
public <T> AsyncResource<T> showForResult(int duration)#showForResult with a custom slide duration.
Since 7.0
finish
public void finish(Object result)Completes the result resource returned by #showForResult and dismisses the
sheet. No-op (besides dismissal) if showForResult was not used to open
this sheet.
Since 7.0
Parameters
resultObject- the value to deliver to the resource subscriber. May be null, in which case subscribers see the same outcome as a user-initiated dismissal.
show
public void show(int duration)Shows the sheet over the current form using a slide-up transition with given duration in milliseconds.
If another sheet is currently being shown, then this will replace that sheet, and use an appropriate slide animation to adjust the size.
Parameters
durationint- The duration of the slide transition in milliseconds.
See also
getPosition
public String getPosition()BorderLayout#CENTER, BorderLayout#NORTH, BorderLayout#SOUTH,
BorderLayout#WEST, or BorderLayout#EAST. Default is BorderLayout#SOUTH.setPosition
public void setPosition(String position)BorderLayout#CENTER, BorderLayout#NORTH, BorderLayout#SOUTH,
BorderLayout#WEST, or BorderLayout#EAST. Default is BorderLayout#SOUTH.Parameters
positionString- One of
BorderLayout#CENTER,BorderLayout#NORTH,BorderLayout#SOUTH,BorderLayout#WEST, orBorderLayout#EAST.
setPosition
public void setPosition(String phonePosition, String tabletPosition)BorderLayout#CENTER, BorderLayout#NORTH, BorderLayout#SOUTH,
BorderLayout#WEST, or BorderLayout#EAST. Default is BorderLayout#SOUTH.Parameters
phonePositionString- Position to use on a phone (i.e. non-tablet). One of
BorderLayout#CENTER,BorderLayout#NORTH,BorderLayout#SOUTH,BorderLayout#WEST, orBorderLayout#EAST. tabletPositionString- Position to use on a tablet and desktop. One of
BorderLayout#CENTER,BorderLayout#NORTH,BorderLayout#SOUTH,BorderLayout#WEST, orBorderLayout#EAST.
back
public void back()See also
back
public void back(int duration)Parameters
durationint- Duration of the slide transition in milliseconds.
setX
public void setX(int x)Parameters
xint- the current x coordinate of the components origin
setY
public void setY(int y)Parameters
yint- the current y coordinate of the components origin
setWidth
public void setWidth(int width)Sets the Component width, this method is exposed for the purpose of external layout managers and should not be invoked directly.
If a user wishes to affect the component size, setPreferredSize should be used.
Parameters
widthint- the width of the component
See also
setHeight
public void setHeight(int height)Sets the Component height, this method is exposed for the purpose of external layout managers and should not be invoked directly.
If a user wishes to affect the component size, setPreferredSize should be used.
Parameters
heightint- the height of the component
See also
getParentSheet
public Sheet getParentSheet()Returns
initComponent
protected void initComponent()deinitialize
protected void deinitialize()isAncestorSheetOf
public boolean isAncestorSheetOf(Sheet sheet)Parameters
sheetSheet- The sheet to check
Returns
addCloseListener
public void addCloseListener(ActionListener l)removeCloseListener
public void removeCloseListener(ActionListener l)Parameters
lActionListener- The close listener
addBackListener
public void addBackListener(ActionListener l)Parameters
lActionListener- Listener
removeBackListener
public void removeBackListener(ActionListener l)Parameters
lActionListener- The close listener