Class KeyEvents


  • public final class KeyEvents
    extends Object
    A utility class for key events.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addKeyEvent​(JComponent component, int keyEvent, int modifiers, int condition, boolean onKeyRelease, Action action)
      Links the given action to the given key event on the given component via inputMap/actionMap, using the name of the action as key for the actionMap, if action is null the binding is removed
      static void addKeyEvent​(JComponent component, int keyEvent, int modifiers, int condition, Action action)
      Links the given action to the given key event on the given component via inputMap/actionMap, using the name of the action as key for the actionMap and true for onKeyRelease.
      static void addKeyEvent​(JComponent component, int keyEvent, int modifiers, Action action)
      Links the given action to the given key event on the given component via inputMap/actionMap, using the name of the action as key for the actionMap, JComponent.WHEN_FOCUSED as condition and true for onKeyRelease.
      static void addKeyEvent​(JComponent component, int keyEvent, Action action)
      Links the given action to the given key event on the given component via inputMap/actionMap, using the name of the action as key for the actionMap, JComponent.WHEN_FOCUSED as condition, 0 as modifier and true for onKeyRelease.
      static <T extends JTextComponent>
      T
      removeTransferFocusOnEnter​(T component)
      Removes the transfer focus action added via transferFocusOnEnter(JComponent)
      static Action transferFocusBackwardAction​(JComponent component)
      Instantiates an Action for transferring keyboard focus backward.
      static Action transferFocusForwardAction​(JComponent component)
      Instantiates an Action for transferring keyboard focus forward.
      static <T extends JComponent>
      T
      transferFocusOnEnter​(T component)
      Adds a key event to the component which transfers focus on enter, and backwards if shift is down
    • Method Detail

      • addKeyEvent

        public static void addKeyEvent​(JComponent component,
                                       int keyEvent,
                                       Action action)
        Links the given action to the given key event on the given component via inputMap/actionMap, using the name of the action as key for the actionMap, JComponent.WHEN_FOCUSED as condition, 0 as modifier and true for onKeyRelease.
        Parameters:
        component - the component
        keyEvent - the key event
        action - the action
        Throws:
        NullPointerException - in case component, action or the action name is null
        See Also:
        KeyStroke.getKeyStroke(int, int, boolean)
      • addKeyEvent

        public static void addKeyEvent​(JComponent component,
                                       int keyEvent,
                                       int modifiers,
                                       Action action)
        Links the given action to the given key event on the given component via inputMap/actionMap, using the name of the action as key for the actionMap, JComponent.WHEN_FOCUSED as condition and true for onKeyRelease.
        Parameters:
        component - the component
        keyEvent - the key event
        modifiers - the modifiers
        action - the action
        Throws:
        NullPointerException - in case component, action or the action name is null
        See Also:
        KeyStroke.getKeyStroke(int, int, boolean)
      • addKeyEvent

        public static void addKeyEvent​(JComponent component,
                                       int keyEvent,
                                       int modifiers,
                                       int condition,
                                       Action action)
        Links the given action to the given key event on the given component via inputMap/actionMap, using the name of the action as key for the actionMap and true for onKeyRelease.
        Parameters:
        component - the component
        keyEvent - the key event
        modifiers - the modifiers
        condition - the condition
        action - the action
        Throws:
        NullPointerException - in case component, action or the action name is null
        See Also:
        KeyStroke.getKeyStroke(int, int, boolean)
      • addKeyEvent

        public static void addKeyEvent​(JComponent component,
                                       int keyEvent,
                                       int modifiers,
                                       int condition,
                                       boolean onKeyRelease,
                                       Action action)
        Links the given action to the given key event on the given component via inputMap/actionMap, using the name of the action as key for the actionMap, if action is null the binding is removed
        Parameters:
        component - the component
        keyEvent - the key event
        modifiers - the modifiers
        condition - the condition
        onKeyRelease - the onKeyRelease condition
        action - the action, if null then the action binding is removed
        See Also:
        KeyStroke.getKeyStroke(int, int, boolean)
      • transferFocusOnEnter

        public static <T extends JComponent> T transferFocusOnEnter​(T component)
        Adds a key event to the component which transfers focus on enter, and backwards if shift is down
        Type Parameters:
        T - the component type
        Parameters:
        component - the component
        Returns:
        the component
        See Also:
        removeTransferFocusOnEnter(JTextComponent)
      • removeTransferFocusOnEnter

        public static <T extends JTextComponent> T removeTransferFocusOnEnter​(T component)
        Removes the transfer focus action added via transferFocusOnEnter(JComponent)
        Type Parameters:
        T - the component type
        Parameters:
        component - the component
        Returns:
        the component
      • transferFocusForwardAction

        public static Action transferFocusForwardAction​(JComponent component)
        Instantiates an Action for transferring keyboard focus forward.
        Parameters:
        component - the component
        Returns:
        an Action for transferring focus
      • transferFocusBackwardAction

        public static Action transferFocusBackwardAction​(JComponent component)
        Instantiates an Action for transferring keyboard focus backward.
        Parameters:
        component - the component
        Returns:
        an Action for transferring focus