Class Command

java.lang.Object
com.codename1.ui.Command
All Implemented Interfaces:
ActionListener<ActionEvent>
Direct Known Subclasses:
NavigationCommand, ShareService

public class Command extends java.lang.Object implements ActionListener<ActionEvent>
The Command class provides a useful extension to the ActionListener interface in cases where the same functionality may be accessed by several controls.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Client property key carrying the desktop native-menu placement hint for this command.
    static final String
    Standard desktop menu placement: an "About" item, conventionally in the application menu.
    static final String
    Standard desktop menu placement: the application menu (macOS app menu / a leading menu).
    static final String
    Standard desktop menu placement: the Edit menu.
    static final String
    Standard desktop menu placement: the File menu.
    static final String
    Standard desktop menu placement: the Help menu.
    static final String
    Standard desktop menu placement: a "Preferences"/"Settings" item in the application menu.
    static final String
    Standard desktop menu placement: a "Quit"/"Exit" item in the application menu.
    static final String
    Standard desktop menu placement: the View menu.
    static final String
    Standard desktop menu placement: the Window menu.
    static final String
    Client property key carrying the upper-cased character of the desktop keyboard accelerator (as an Integer), see #setDesktopShortcut(char, int).
    static final int
    Accelerator modifier flag for the Alt/Option key.
    static final int
    Accelerator modifier flag for the platform's primary command key: Command on macOS, Control on Windows/Linux.
    static final int
    Accelerator modifier flag for the Shift key.
    static final String
    Client property key carrying the modifier bit-mask of the desktop keyboard accelerator (as an Integer), see #setDesktopShortcut(char, int).
  • Constructor Summary

    Constructors
    Constructor
    Description
    Command(String command)
    Creates a new instance of Command
    Command(String command, int id)
    Creates a new instance of Command
    Command(String command, Image icon)
    Creates a new instance of Command
    Command(String command, Image icon, int id)
    Creates a new instance of Command
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    This method is called when the soft button/Menu item is clicked
    static Command
    create(String name, Image icon, ActionListener ev)
    Creates a new command instance that encapsulates the action listener and details, the main value of this approach is in our ability to write commands using the shorthand lambda syntax of Java 8.
    static Command
    createMaterial(String name, char icon, ActionListener ev)
    Creates a new command instance that encapsulates the action listener and details, the main value of this approach is in our ability to write commands using the shorthand lambda syntax of Java 8.
    boolean
    equals(java.lang.Object obj)
    compare two commands
    java.lang.Object
    The client properties are a useful way to associate meta-data with a command without subclassing
    gets the Command Name
    Returns the desktop native-menu placement hint, or null when unset.
    int
    Returns the upper-cased accelerator character assigned via #setDesktopShortcut(char, int), or 0 when no accelerator is set.
    int
    Returns the accelerator modifier bit-mask assigned via #setDesktopShortcut(char, int), or 0 when no accelerator is set.
    Indicates the icon that is displayed on the button when the button is in the disabled state
    Returns the icon representing the command
    Returns
    float
    The gap between the text and the icon in millimeters or -1 for default
    int
    Return the command ID
    char
    Returns
    float
    Returns
    Indicates the icon that is displayed on the button when the button is in pressed state
    Indicates the icon that is displayed on the button when the button is in rolled over state
    int
    Allows storing commands in a vector/hashtable
    boolean
    Indicates whether this command causes the dialog to dispose implicitly, defaults to true
    boolean
    Allows disabling/enabling the command
    void
    putClientProperty(String key, java.lang.Object value)
    The client properties are a useful way to associate meta-data with a command without sub classing
    void
    sets the Command name
    Sets the desktop native-menu placement hint for this command.
    setDesktopShortcut(char keyChar)
    Assigns a keyboard accelerator to this command for the desktop native menu bar using the platform's primary modifier (Command on macOS, Control elsewhere).
    setDesktopShortcut(char keyChar, int modifiers)
    Assigns a keyboard accelerator to this command for the desktop native menu bar.
    void
    setDisabledIcon(Image disabledIcon)
    Indicates the icon that is displayed on the button when the button is in the disabled state
    void
    setDisposesDialog(boolean disposesDialog)
    Indicates whether this command causes the dialog to dispose implicitly, defaults to true
    void
    setEnabled(boolean enabled)
    Allows disabling/enabling the command
    void
    setIcon(Image icon)
    Sets the icon for the command
    void
    setIconFont(Font iconFont)
    Parameters
    void
    setIconGapMM(float iconGapMM)
    The gap between the text and the icon in millimeters or -1 for default
    void
    setMaterialIcon(char materialIcon)
    Parameters
    void
    setMaterialIconSize(float materialIconSize)
    Parameters
    void
    setPressedIcon(Image pressedIcon)
    Indicates the icon that is displayed on the button when the button is in pressed state
    void
    setRolloverIcon(Image rolloverIcon)
    Indicates the icon that is displayed on the button when the button is in rolled over state
    Returns a string representation of the object

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • DESKTOP_MENU

      public static final String DESKTOP_MENU
      Client property key carrying the desktop native-menu placement hint for this command. When a desktop app bridges its commands to the native menu bar (title-bar mode native/custom), the value selects which menu the command appears under. Recognized values (case-insensitive): #DESKTOP_MENU_APP, #DESKTOP_MENU_ABOUT, #DESKTOP_MENU_PREFERENCES, #DESKTOP_MENU_QUIT, #DESKTOP_MENU_FILE, #DESKTOP_MENU_EDIT, #DESKTOP_MENU_VIEW, #DESKTOP_MENU_WINDOW, #DESKTOP_MENU_HELP. Any other value becomes a top-level menu with that literal title. Commands without the hint are grouped under a default application-commands menu.
      See Also:
    • DESKTOP_MENU_APP

      public static final String DESKTOP_MENU_APP
      Standard desktop menu placement: the application menu (macOS app menu / a leading menu).
      See Also:
    • DESKTOP_MENU_ABOUT

      public static final String DESKTOP_MENU_ABOUT
      Standard desktop menu placement: an "About" item, conventionally in the application menu.
      See Also:
    • DESKTOP_MENU_PREFERENCES

      public static final String DESKTOP_MENU_PREFERENCES
      Standard desktop menu placement: a "Preferences"/"Settings" item in the application menu.
      See Also:
    • DESKTOP_MENU_QUIT

      public static final String DESKTOP_MENU_QUIT
      Standard desktop menu placement: a "Quit"/"Exit" item in the application menu.
      See Also:
    • DESKTOP_MENU_FILE

      public static final String DESKTOP_MENU_FILE
      Standard desktop menu placement: the File menu.
      See Also:
    • DESKTOP_MENU_EDIT

      public static final String DESKTOP_MENU_EDIT
      Standard desktop menu placement: the Edit menu.
      See Also:
    • DESKTOP_MENU_VIEW

      public static final String DESKTOP_MENU_VIEW
      Standard desktop menu placement: the View menu.
      See Also:
    • DESKTOP_MENU_WINDOW

      public static final String DESKTOP_MENU_WINDOW
      Standard desktop menu placement: the Window menu.
      See Also:
    • DESKTOP_MENU_HELP

      public static final String DESKTOP_MENU_HELP
      Standard desktop menu placement: the Help menu.
      See Also:
    • DESKTOP_SHORTCUT_KEY

      public static final String DESKTOP_SHORTCUT_KEY
      Client property key carrying the upper-cased character of the desktop keyboard accelerator (as an Integer), see #setDesktopShortcut(char, int).
      See Also:
    • DESKTOP_SHORTCUT_MODIFIERS

      public static final String DESKTOP_SHORTCUT_MODIFIERS
      Client property key carrying the modifier bit-mask of the desktop keyboard accelerator (as an Integer), see #setDesktopShortcut(char, int).
      See Also:
    • DESKTOP_SHORTCUT_MODIFIER_PRIMARY

      public static final int DESKTOP_SHORTCUT_MODIFIER_PRIMARY
      Accelerator modifier flag for the platform's primary command key: Command on macOS, Control on Windows/Linux. This is the modifier used by the vast majority of menu shortcuts so it's the default.
      See Also:
    • DESKTOP_SHORTCUT_MODIFIER_SHIFT

      public static final int DESKTOP_SHORTCUT_MODIFIER_SHIFT
      Accelerator modifier flag for the Shift key.
      See Also:
    • DESKTOP_SHORTCUT_MODIFIER_ALT

      public static final int DESKTOP_SHORTCUT_MODIFIER_ALT
      Accelerator modifier flag for the Alt/Option key.
      See Also:
  • Constructor Details

    • Command

      public Command(String command)

      Creates a new instance of Command

      Parameters
      • command: the string that will be placed on the Soft buttons\Menu
    • Command

      public Command(String command, Image icon)

      Creates a new instance of Command

      Parameters
      • command: the string that will be placed on the Soft buttons\Menu

      • icon: the icon representing the command

    • Command

      public Command(String command, int id)

      Creates a new instance of Command

      Parameters
      • command: the string that will be placed on the Soft buttons\Menu

      • id: @param id user defined ID for a command simplifying switch statement code working with a command

    • Command

      public Command(String command, Image icon, int id)

      Creates a new instance of Command

      Parameters
      • command: the string that will be placed on the Soft buttons\Menu

      • icon: the icon representing the command

      • id: @param id user defined ID for a command simplifying switch statement code working with a command

  • Method Details

    • create

      public static Command create(String name, Image icon, ActionListener ev)

      Creates a new command instance that encapsulates the action listener and details, the main value of this approach is in our ability to write commands using the shorthand lambda syntax of Java 8.

      Parameters
      • name: the name/title of the command

      • icon: the icon for the command

      • ev: the even handler

      Returns

      a newly created Command instance

    • createMaterial

      public static Command createMaterial(String name, char icon, ActionListener ev)

      Creates a new command instance that encapsulates the action listener and details, the main value of this approach is in our ability to write commands using the shorthand lambda syntax of Java 8.

      Parameters
      • name: the name/title of the command

      • icon: the icon for the command

      • ev: the even handler

      Returns

      a newly created Command instance

      Since

      6.0

    • getId

      public int getId()

      Return the command ID

      Returns

      the command ID

    • getCommandName

      public String getCommandName()

      gets the Command Name

      Returns

      the Command name

    • setCommandName

      public void setCommandName(String command)

      sets the Command name

      Parameters
      • command
    • getIcon

      public Image getIcon()

      Returns the icon representing the command

      Returns

      an icon representing the command

    • setIcon

      public void setIcon(Image icon)

      Sets the icon for the command

      Parameters
      • icon: the new icon
    • toString

      public String toString()

      Returns a string representation of the object

      Returns

      Returns a string representation of the object

      Overrides:
      toString in class java.lang.Object
    • getPressedIcon

      public Image getPressedIcon()

      Indicates the icon that is displayed on the button when the button is in pressed state

      Returns

      icon used

    • setPressedIcon

      public void setPressedIcon(Image pressedIcon)

      Indicates the icon that is displayed on the button when the button is in pressed state

      Parameters
      • pressedIcon: icon used
    • getDisabledIcon

      public Image getDisabledIcon()

      Indicates the icon that is displayed on the button when the button is in the disabled state

      Returns

      icon used

    • setDisabledIcon

      public void setDisabledIcon(Image disabledIcon)

      Indicates the icon that is displayed on the button when the button is in the disabled state

      Parameters
      • disabledIcon: icon used
    • getRolloverIcon

      public Image getRolloverIcon()

      Indicates the icon that is displayed on the button when the button is in rolled over state

      Returns

      icon used

    • setRolloverIcon

      public void setRolloverIcon(Image rolloverIcon)

      Indicates the icon that is displayed on the button when the button is in rolled over state

      Parameters
      • rolloverIcon: icon to use
    • equals

      public boolean equals(java.lang.Object obj)

      compare two commands

      Parameters
      • obj: a Command Object to compare
      Returns

      true if the obj has the same command name

      Overrides:
      equals in class java.lang.Object
    • hashCode

      public int hashCode()

      Allows storing commands in a vector/hashtable

      Returns

      unique hashcode for the command class

      Overrides:
      hashCode in class java.lang.Object
    • actionPerformed

      public void actionPerformed(ActionEvent evt)

      This method is called when the soft button/Menu item is clicked

      Parameters
      • evt: the Event Object
      Specified by:
      actionPerformed in interface ActionListener<ActionEvent>
    • isDisposesDialog

      public boolean isDisposesDialog()
      Indicates whether this command causes the dialog to dispose implicitly, defaults to true
    • setDisposesDialog

      public void setDisposesDialog(boolean disposesDialog)
      Indicates whether this command causes the dialog to dispose implicitly, defaults to true
    • isEnabled

      public boolean isEnabled()

      Allows disabling/enabling the command

      Returns

      the enabled

    • setEnabled

      public void setEnabled(boolean enabled)

      Allows disabling/enabling the command

      Parameters
      • enabled: the enabled to set
    • getClientProperty

      public java.lang.Object getClientProperty(String key)

      The client properties are a useful way to associate meta-data with a command without subclassing

      Parameters
      • key: an arbitrary user key
      Returns

      an arbitrary user object

    • putClientProperty

      public void putClientProperty(String key, java.lang.Object value)

      The client properties are a useful way to associate meta-data with a command without sub classing

      Parameters
      • key: an arbitrary user key

      • value: an arbitrary user object, null to remove

    • setDesktopMenu

      public Command setDesktopMenu(String menu)

      Sets the desktop native-menu placement hint for this command. See #DESKTOP_MENU.

      Parameters
      • menu: one of the DESKTOP_MENU_* constants or a custom top-level menu title
      Returns

      this command, for chaining

    • getDesktopMenu

      public String getDesktopMenu()

      Returns the desktop native-menu placement hint, or null when unset. See #DESKTOP_MENU.

      Returns

      the placement hint or null

    • setDesktopShortcut

      public Command setDesktopShortcut(char keyChar)

      Assigns a keyboard accelerator to this command for the desktop native menu bar using the platform's primary modifier (Command on macOS, Control elsewhere). For example setDesktopShortcut('S') produces Cmd+S / Ctrl+S next to the menu item. The hint is inert on platforms without a native menu bar, so it's safe to set unconditionally.

      Parameters
      • keyChar: the accelerator character (case-insensitive)
      Returns

      this command, for chaining

    • setDesktopShortcut

      public Command setDesktopShortcut(char keyChar, int modifiers)

      Assigns a keyboard accelerator to this command for the desktop native menu bar. See #setDesktopShortcut(char) for the common case.

      Parameters
      • keyChar: the accelerator character (case-insensitive)

      • modifiers: a bit-mask of #DESKTOP_SHORTCUT_MODIFIER_PRIMARY, #DESKTOP_SHORTCUT_MODIFIER_SHIFT and #DESKTOP_SHORTCUT_MODIFIER_ALT

      Returns

      this command, for chaining

    • getDesktopShortcutKeyChar

      public int getDesktopShortcutKeyChar()

      Returns the upper-cased accelerator character assigned via #setDesktopShortcut(char, int), or 0 when no accelerator is set.

      Returns

      the accelerator character or 0

    • getDesktopShortcutModifiers

      public int getDesktopShortcutModifiers()

      Returns the accelerator modifier bit-mask assigned via #setDesktopShortcut(char, int), or 0 when no accelerator is set.

      Returns

      the modifier bit-mask

    • getMaterialIcon

      public char getMaterialIcon()
      Returns

      the materialIcon

    • setMaterialIcon

      public void setMaterialIcon(char materialIcon)
      Parameters
      • materialIcon: the materialIcon to set
    • getIconGapMM

      public float getIconGapMM()

      The gap between the text and the icon in millimeters or -1 for default

      Returns

      the iconGapMM

    • setIconGapMM

      public void setIconGapMM(float iconGapMM)

      The gap between the text and the icon in millimeters or -1 for default

      Parameters
      • iconGapMM: the iconGapMM to set
    • getMaterialIconSize

      public float getMaterialIconSize()
      Returns

      the materialIconSize

    • setMaterialIconSize

      public void setMaterialIconSize(float materialIconSize)
      Parameters
      • materialIconSize: the materialIconSize to set
    • getIconFont

      public Font getIconFont()
      Returns

      the set iconFont or null if none defined (meaning for material icons instead of the system default MaterialDesign font

    • setIconFont

      public void setIconFont(Font iconFont)
      Parameters
      • iconFont: use iconFont for material icons instead of the system default MaterialDesign font