Class Picker

All Implemented Interfaces:
Animation, Editable, ActionSource<ActionEvent>, StyleListener, IconHolder, ReleasableComponent, SelectableIconHolder, TextHolder

public class Picker extends Button

Picker is a component and API that allows either popping up a spinner or using the native picker API when applicable. This is quite important for some platforms where the native spinner behavior is very hard to replicate.

Form hi = new Form("Picker", new BoxLayout(BoxLayout.Y_AXIS));
Picker datePicker = new Picker();
datePicker.setType(Display.PICKER_TYPE_DATE);
Picker dateTimePicker = new Picker();
dateTimePicker.setType(Display.PICKER_TYPE_DATE_AND_TIME);
Picker timePicker = new Picker();
timePicker.setType(Display.PICKER_TYPE_TIME);
Picker stringPicker = new Picker();
stringPicker.setType(Display.PICKER_TYPE_STRINGS);

datePicker.setDate(new Date());
dateTimePicker.setDate(new Date());
timePicker.setTime(10 * 60); // 10:00AM = Minutes since midnight
stringPicker.setStrings("A Game of Thrones", "A Clash Of Kings", "A Storm Of Swords", "A Feast For Crows",
        "A Dance With Dragons", "The Winds of Winter", "A Dream of Spring");
stringPicker.setSelectedString("A Game of Thrones");

hi.add(datePicker).add(dateTimePicker).add(timePicker).add(stringPicker);
hi.show();
  • Constructor Details

    • Picker

      public Picker()
      Default constructor
  • Method Details

    • isDefaultUseLightweightPopup

      public static boolean isDefaultUseLightweightPopup()

      Whether useLightweightPopup should default to true, this can be set via the theme constant lightweightPickerBool

      Returns

      the defaultUseLightweightPopup

    • setDefaultUseLightweightPopup

      public static void setDefaultUseLightweightPopup(boolean aDefaultUseLightweightPopup)

      Whether useLightweightPopup should default to true, this can be set via the theme constant lightweightPickerBool

      Parameters
      • aDefaultUseLightweightPopup: the defaultUseLightweightPopup to set
    • setHourRange

      public void setHourRange(int min, int max)

      Sets the hour range for this picker. Only applicable for types Display#PICKER_TYPE_DATE_AND_TIME and Display#PICKER_TYPE_TIME. Also, only applicable to lightweight picker (i.e. #isUseLightweightPopup() == true.

      Parameters
      • min: The minimum hour to display (0-24) or -1 for no limit.

      • max: The maximum hour to display (0-24) or -1 for no limit

      Since

      6.0

      See also
      • #getMinHour()

      • #getMaxHour()

    • getMinHour

      public int getMinHour()

      Gets the minimum hour to show for time and datetime pickers. Only applicable for types Display#PICKER_TYPE_DATE_AND_TIME and Display#PICKER_TYPE_TIME. Also, only applicable to lightweight picker (i.e. #isUseLightweightPopup() == true.

      Returns

      The minimum hour. 0-24, or -1 for no limit.

      Since

      6.0

      See also
      • #getMaxHour()

      • #setHourRange(int, int)

    • getMaxHour

      public int getMaxHour()

      Gets the minimum hour to show for time and datetime pickers. Only applicable for types Display#PICKER_TYPE_DATE_AND_TIME and Display#PICKER_TYPE_TIME. Also, only applicable to lightweight picker (i.e. #isUseLightweightPopup() == true.

      Returns

      The minimum hour. 0-24, or -1 for no limit.

      Since

      6.0

      See also
      • #getMinHour()

      • #setHourRange(int, int)

    • getStartDate

      public Date getStartDate()

      Gets the start date of the picker. Only applicable for types Display#PICKER_TYPE_DATE_AND_TIME and Display#PICKER_TYPE_DATE. Also, only applicable to lightweight picker (i.e. #isUseLightweightPopup() == true.

      Returns
      Returns:

      The start date or null if there is none set.

      This does not apply to the time. Only the date. You can set the hour range using int).

      Since

      6.0

      See also
      • #getEndDate()

      • #setStartDate(java.util.Date)

      • #getMinHour()

    • setStartDate

      public void setStartDate(Date start)

      Sets the start date of the picker. Only applicable for types Display#PICKER_TYPE_DATE_AND_TIME and Display#PICKER_TYPE_DATE. Also, only applicable to lightweight picker (i.e. #isUseLightweightPopup() == true.

      This does not affect the time. Only the date. You can set the hour range using int).

      Parameters
      • start: The start date.
      Since

      6.0

      See also
      • #getStartDate()

      • #setEndDate(java.util.Date)

    • getEndDate

      public Date getEndDate()

      Gets the end date of the picker. Only applicable for types Display#PICKER_TYPE_DATE_AND_TIME and Display#PICKER_TYPE_DATE. Also, only applicable to lightweight picker (i.e. #isUseLightweightPopup() == true.

      Returns
      Returns:

      The end date or null if there is none set.

      This does not apply to the time. Only the date. You can set the hour range using int).

      Since

      6.0

      See also
      • #getStartDate()

      • #setEndDate(java.util.Date)

      • #getMaxHour()

    • setEndDate

      public void setEndDate(Date end)

      Sets the end date of the picker. Only applicable for types Display#PICKER_TYPE_DATE_AND_TIME and Display#PICKER_TYPE_DATE. Also, only applicable to lightweight picker (i.e. #isUseLightweightPopup() == true.

      This does not affect the time. Only the date. You can set the hour range using int).

      Parameters
      • end: The end date.
      Since

      6.0

      See also
      • #setStartDate(java.util.Date)

      • #getEndDate()

    • isUseLightweightPopup

      public boolean isUseLightweightPopup()
      Checks if this picker is in lightweight mode. If this returns true, then the picker will use cross-platform lightweight widgets instead of native widgets.
    • setUseLightweightPopup

      public void setUseLightweightPopup(boolean useLightweightPopup)

      Sets the picker to use lightweight mode for its widgets. With this mode enabled the picker will use cross-platform lightweight widgets instead of native widgets.

      Parameters
      • useLightweightPopup
    • startEditingAsync

      public void startEditingAsync()
      Description copied from class: Component

      If the component #isEditable(), then this will start the editing process. For TextFields, this results in showing the keyboard and allowing the user to edit the input. For the Picker, this will display the popup.

      See also
      • #stopEditing(java.lang.Runnable)

      • #isEditing()

      • #isEditable()

      • #getEditingDelegate()

      • #setEditingDelegate(com.codename1.ui.Editable)

      Specified by:
      startEditingAsync in interface Editable
      Overrides:
      startEditingAsync in class Component
    • stopEditing

      public void stopEditing(Runnable onFinish)
      Description copied from class: Component

      Stops the editing process.

      Parameters
      • onFinish: Callback called when the editing is complete.
      See also
      • #startEditingAsync()

      • #isEditing()

      • #isEditable()

      • #getEditingDelegate()

      • #setEditingDelegate(com.codename1.ui.Editable)

      Specified by:
      stopEditing in interface Editable
      Overrides:
      stopEditing in class Component
    • isEditing

      public boolean isEditing()
      Description copied from class: Component

      Checks if the component is currently being edited.

      Returns

      True if the component is currently being edited.

      See also
      • #startEditingAsync()

      • #stopEditing(java.lang.Runnable)

      • #isEditable()

      • #getEditingDelegate()

      • #setEditingDelegate(com.codename1.ui.Editable)

      Specified by:
      isEditing in interface Editable
      Overrides:
      isEditing in class Component
    • isEditable

      public boolean isEditable()
      Description copied from class: Component

      Checks to see if the component is editable. This is used for next/previous focus traversal on forms.

      See also
      • #getEditingDelegate()

      • #setEditingDelegate(com.codename1.ui.Editable)

      • #isEditing()

      • #startEditingAsync()

      • #stopEditing(java.lang.Runnable)

      Specified by:
      isEditable in interface Editable
      Overrides:
      isEditable in class Component
    • getType

      public int getType()

      Returns the type of the picker

      Returns
      Returns:
      one of Display.PICKER_TYPE_DATE, Display.PICKER_TYPE_DATE_AND_TIME, Display.PICKER_TYPE_STRINGS, Display.PICKER_TYPE_DURATION, Display.PICKER_TYPE_DURATION_HOURS, Display.PICKER_TYPE_DURATION_MINUTES, or Display.PICKER_TYPE_TIME
    • setType

      public void setType(int type)

      Sets the type of the picker to one of Display.PICKER_TYPE_DATE, Display.PICKER_TYPE_DATE_AND_TIME, Display.PICKER_TYPE_STRINGS, Display.PICKER_TYPE_DURATION, Display.PICKER_TYPE_DURATION_HOURS, Display.PICKER_TYPE_DURATION_MINUTES or Display.PICKER_TYPE_TIME

      Parameters
      • type: the type
    • getDate

      public Date getDate()

      Returns the date, this value is used both for type date/date and time. Notice that this value isn't used for time

      Returns

      the date object

    • setDate

      public void setDate(Date d)

      Sets the date, this value is used both for type date/date and time. Notice that this value isn't used for time. Notice that this value will have no effect if the picker is currently showing.

      Parameters
      • d: the new date
    • getStrings

      public String[] getStrings()

      Returns the String array matching the metadata

      Returns

      a string array

    • setStrings

      public void setStrings(String... strs)

      Sets the string entries for the string picker.

      sample usage for this method below:

      Toolbar.setGlobalToolbar(true);
      Form hi = new Form("Transitions", new BoxLayout(BoxLayout.Y_AXIS));
      Style bg = hi.getContentPane().getUnselectedStyle();
      bg.setBgTransparency(255);
      bg.setBgColor(0xff0000);
      Button showTransition = new Button("Show");
      Picker pick = new Picker();
      pick.setStrings("Slide", "SlideFade", "Cover", "Uncover", "Fade", "Flip");
      pick.setSelectedString("Slide");
      TextField duration = new TextField("10000", "Duration", 6, TextArea.NUMERIC);
      CheckBox horizontal = CheckBox.createToggle("Horizontal");
      pick.addActionListener((e) -> {
          String s = pick.getSelectedString().toLowerCase();
          horizontal.setEnabled(s.equals("slide") || s.indexOf("cover") > -1);
      });
      horizontal.setSelected(true);
      hi.add(showTransition).
          add(pick).
          add(duration).
          add(horizontal);
      
      Form dest = new Form("Destination");
      bg = dest.getContentPane().getUnselectedStyle();
      bg.setBgTransparency(255);
      bg.setBgColor(0xff);
      dest.setBackCommand(
              dest.getToolbar().addCommandToLeftBar("Back", null, (e) -> hi.showBack()));
      
      showTransition.addActionListener((e) -> {
          int h = CommonTransitions.SLIDE_HORIZONTAL;
          if(!horizontal.isSelected()) {
              h = CommonTransitions.SLIDE_VERTICAL;
          }
          switch(pick.getSelectedString()) {
              case "Slide":
                  hi.setTransitionOutAnimator(CommonTransitions.createSlide(h, true, duration.getAsInt(3000)));
                  dest.setTransitionOutAnimator(CommonTransitions.createSlide(h, true, duration.getAsInt(3000)));
                  break;
              case "SlideFade":
                  hi.setTransitionOutAnimator(CommonTransitions.createSlideFadeTitle(true, duration.getAsInt(3000)));
                  dest.setTransitionOutAnimator(CommonTransitions.createSlideFadeTitle(true, duration.getAsInt(3000)));
                  break;
              case "Cover":
                  hi.setTransitionOutAnimator(CommonTransitions.createCover(h, true, duration.getAsInt(3000)));
                  dest.setTransitionOutAnimator(CommonTransitions.createCover(h, true, duration.getAsInt(3000)));
                  break;
              case "Uncover":
                  hi.setTransitionOutAnimator(CommonTransitions.createUncover(h, true, duration.getAsInt(3000)));
                  dest.setTransitionOutAnimator(CommonTransitions.createUncover(h, true, duration.getAsInt(3000)));
                  break;
              case "Fade":
                  hi.setTransitionOutAnimator(CommonTransitions.createFade(duration.getAsInt(3000)));
                  dest.setTransitionOutAnimator(CommonTransitions.createFade(duration.getAsInt(3000)));
                  break;
              case "Flip":
                  hi.setTransitionOutAnimator(new FlipTransition(-1, duration.getAsInt(3000)));
                  dest.setTransitionOutAnimator(new FlipTransition(-1, duration.getAsInt(3000)));
                  break;
          }
          dest.show();
      });
      hi.show();
      
      Parameters
      • strs: string array
    • getSelectedString

      public String getSelectedString()

      Returns the current string

      Returns

      the selected string

    • setSelectedString

      public void setSelectedString(String str)

      Sets the current value in a string array picker

      Parameters
      • str: the current value
    • getSelectedStringIndex

      public int getSelectedStringIndex()

      Returns the index of the selected string

      Returns

      the selected string offset or -1

    • setSelectedStringIndex

      public void setSelectedStringIndex(int index)

      Returns the index of the selected string

      Parameters
      • index: sets the index of the selected string
    • updateValue

      protected void updateValue()
      Updates the display value of the picker, subclasses can override this to invoke set text with the right value
    • setTime

      public void setTime(int hour, int minute)

      Convenience method equivalent to invoking setTime(hour * 60 + minute);

      Parameters
      • hour: the hour in 24hr format

      • minute: the minute within the hour

    • getTime

      public int getTime()

      This value is only used for time type and is ignored in the case of date and time where both are embedded within the date.

      Returns

      the time value as minutes since midnight e.g. 630 is 10:30am

    • setTime

      public void setTime(int time)

      This value is only used for time type and is ignored in the case of date and time where both are embedded within the date.

      Parameters
      • time: the time value as minutes since midnight e.g. 630 is 10:30am
    • setMinuteStep

      public void setMinuteStep(int step)

      Sets the minute step size for PICKER_TYPE_DURATION, and PICKER_TYPE_DURATION_TIME types.

      Parameters
      • step: The step size in minutes.
    • setDuration

      public void setDuration(int hour, int minute)

      Convenience method for setting duration in hours and minutes.

      Parameters
      • hour: The hours for duration.

      • minute: The minutes for duration.

      See also
      • #setDuration(long)

      • #getDuration()

      • #getDurationHours()

      • #getDurationMinutes()

    • getDuration

      public long getDuration()

      This value is used for the duration type.

      Returns

      The duration in milliseconds.

      See also
      • #getDurationHours()

      • #getDurationMinutes()

    • setDuration

      public void setDuration(long duration)

      This value is only used for duration type.

      Parameters
      • duration: The duration value in milliseconds.
      See also
      • #setDuration(int, int)

      • #getDuration()

      • #getDurationHours()

      • #getDurationMinutes()

    • getDurationHours

      public int getDurationHours()

      Gets the duration hours. Used only for duration type.

      Returns

      The duration hours.

      See also
      • #getDurationMinutes()

      • #getDuration()

    • getDurationMinutes

      public int getDurationMinutes()

      Gets the duration minutes. Used only for duration type.

      Returns

      The duration minutes.

      See also
      • #getDurationHours()

      • #getDuration()

    • isShowMeridiem

      public boolean isShowMeridiem()

      Indicates whether hours should be rendered as AM/PM or 24hr format

      Returns

      the showMeridiem

    • setShowMeridiem

      public void setShowMeridiem(boolean showMeridiem)

      Indicates whether hours should be rendered as AM/PM or 24hr format

      Parameters
      • showMeridiem: the showMeridiem to set
    • getRenderingPrototype

      public Object getRenderingPrototype()

      When using a lightweight spinner this will be used as the rendering prototype

      Returns

      the renderingPrototype

    • setRenderingPrototype

      public void setRenderingPrototype(Object renderingPrototype)

      When using a lightweight spinner this will be used as the rendering prototype

      Parameters
      • renderingPrototype: the renderingPrototype to set
    • getFormatter

      public SimpleDateFormat getFormatter()

      Allows us to define a date format for the display of dates/times

      Returns

      the defined formatter

    • setFormatter

      public void setFormatter(SimpleDateFormat formatter)

      Allows us to define a date format for the display of dates/times

      Parameters
      • formatter: the new formatter
    • getPreferredPopupWidth

      public int getPreferredPopupWidth()
      The preferred width of the popup dialog. This will only be used on devices where the popup width and height are configurable, such as the iPad or tablets. On iPhone, the picker always spans the width of the screen along the bottom.
    • setPreferredPopupWidth

      public void setPreferredPopupWidth(int width)

      The preferred width of the popup dialog for the picker. This will only be used on devices where the popup width and height are configurable, such as the iPad or tablets. On iPhone, the picker always spans the width of the screen along the bottom.

      Parameters
      • width: The preferred width of the popup.
    • getPreferredPopupHeight

      public int getPreferredPopupHeight()
      The preferred height of the popup dialog. This will only be used on devices where the popup width and height are configurable, such as the iPad or tablets. On iPhone, the picker always spans the width of the screen along the bottom.
    • setPreferredPopupHeight

      public void setPreferredPopupHeight(int height)

      The preferred height of the popup dialog for the picker. This will only be used on devices where the popup width and height are configurable, such as the iPad or tablets. On iPhone, the picker always spans the width of the screen along the bottom.

      Parameters
      • height: The preferred height of the popup.
    • getPropertyNames

      public String[] getPropertyNames()

      A component may expose mutable property names for a UI designer to manipulate, this API is designed for usage internally by the GUI builder code

      Returns

      the property names allowing mutation

      Overrides:
      getPropertyNames in class Label
    • getPropertyTypes

      public Class[] getPropertyTypes()

      Matches the property names method (see that method for further details).

      Returns

      the types of the properties

      Overrides:
      getPropertyTypes in class Label
    • getPropertyTypeNames

      public String[] getPropertyTypeNames()

      This method is here to workaround an XMLVM array type bug where property types aren't identified properly, it returns the names of the types using the following type names: String,int,double,long,byte,short,char,String[],String[][],byte[],Image,Image[],Object[],ListModel,ListCellRenderer

      Returns

      Array of type names

      Overrides:
      getPropertyTypeNames in class Label
    • getPropertyValue

      public Object getPropertyValue(String name)

      Returns the current value of the property name, this method is used by the GUI builder

      Parameters
      • name: the name of the property
      Returns

      the value of said property

      Overrides:
      getPropertyValue in class Label
    • setPropertyValue

      public String setPropertyValue(String name, Object value)

      Sets a new value to the given property, returns an error message if failed and null if successful. Notice that some builtin properties such as "$designMode" might be sent to components to indicate application state.

      Parameters
      • name: the name of the property

      • value: new value for the property

      Returns

      error message or null

      Overrides:
      setPropertyValue in class Label
    • getValue

      public Object getValue()

      Returns the value which works for all picker types

      Returns

      the value object

    • paint

      public void paint(Graphics g)
      Description copied from class: Label

      This method paints the Component on the screen, it should be overriden by subclasses to perform custom drawing or invoke the UI API's to let the PLAF perform the rendering.

      Parameters
      • g: the component graphics
      Specified by:
      paint in interface Animation
      Overrides:
      paint in class Button
    • getStyle

      public Style getStyle()
      Description copied from class: Component

      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.

      Returns

      the component Style object

      Overrides:
      getStyle in class Component