onKeyStroke
Binds a single KeyStroke to onAction via the component's InputMap/ActionMap - the idiomatic Swing path for shortcuts. condition selects the focus scope (a FocusCondition JComponent.WHEN_* value) and defaults to JComponent.WHEN_FOCUSED.
Distinct keystrokes compose independently. Binding the same keyStroke in the same condition twice on one component is reported once the change pass has settled, so two bindings that exchange their keystrokes in one pass are unaffected. onAction is read live, so passing a fresh lambda each recomposition is fine. Requires a JComponent target.
Return
this chain with the key-stroke binding declared on it.
Parameters
the stroke that triggers onAction; a press and a release of the same key are distinct strokes and bind independently.
the focus scope the binding is live in: JComponent.WHEN_FOCUSED only while the component itself holds focus, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT while the component itself or a descendant does, JComponent.WHEN_IN_FOCUSED_WINDOW anywhere in the component's window.
invoked on the event dispatch thread each time the stroke fires in scope.
See also
Convenience overload of onKeyStroke that parses keyStroke via KeyStroke.getKeyStroke(String) (e.g. "ctrl S", "meta shift Z"). Throws at install if the string is not a valid key-stroke descriptor.
Return
this chain with the key-stroke binding declared on it.
Parameters
the key-stroke descriptor; it names a key press unless it says released or typed.
the focus scope the binding is live in, one of the JComponent.WHEN_* values.
invoked on the event dispatch thread each time the stroke fires in scope.