Java Utililty Methods Swing Key Action

List of utility methods to do Swing Key Action

Description

The list of methods to do Swing Key Action are organized into topic(s).

Method

StringgetPrettyStringFor(KeyStroke keyStroke)
Returns a pretty string value for a KeyStroke, suitable for display as the keystroke's value in a GUI.
if (keyStroke == null)
    return "";
String string = KeyEvent.getKeyModifiersText(keyStroke.getModifiers());
if (string != null && string.length() > 0)
    string += "+";
int keyCode = keyStroke.getKeyCode();
if (keyCode != KeyEvent.VK_SHIFT && keyCode != KeyEvent.VK_CONTROL && keyCode != KeyEvent.VK_ALT
        && keyCode != KeyEvent.VK_META)
...
KeyStrokegetSystemHelpKey()
get key that initiates help system
return KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0);
JPanelgetTypePanel(final ActionListener typeListener)
get Type Panel
final JRadioButton fourPlexButton = new JRadioButton("4-plex");
fourPlexButton.setMnemonic(KeyEvent.VK_4);
fourPlexButton.setSelected(true);
fourPlexButton.setActionCommand("4");
final JRadioButton eightPlexButton = new JRadioButton("8-plex");
eightPlexButton.setMnemonic(KeyEvent.VK_8);
eightPlexButton.setActionCommand("8");
final ButtonGroup typeGroup = new ButtonGroup();
...
voidhandleSliderAdjustmentViaKey(KeyEvent e)
Checks if a slider has been adjusted by a keystroke.
if (e.getSource() instanceof JSlider) {
    if (KeyEvent.VK_LEFT == e.getKeyCode() || KeyEvent.VK_RIGHT == e.getKeyCode()
            || KeyEvent.VK_UP == e.getKeyCode() || KeyEvent.VK_DOWN == e.getKeyCode()
            || KeyEvent.VK_PAGE_UP == e.getKeyCode() || KeyEvent.VK_PAGE_DOWN == e.getKeyCode()
            || KeyEvent.VK_HOME == e.getKeyCode() || KeyEvent.VK_END == e.getKeyCode()) {
        JSlider slider = (JSlider) e.getSource();
        for (ChangeListener listener : slider.getChangeListeners()) {
            slider.setValueIsAdjusting(true);
...
JButtoninitButton(String text, String actionKey, int shortcutKey, int modifiers, JComponent component, AbstractAction action)
init Button
setKeyBinding(actionKey, shortcutKey, modifiers, action, component);
JButton button = new JButton(action);
button.setText(text);
button.setMnemonic(shortcutKey);
return button;
voidinstallCloseKey(final RootPaneContainer c)
Install a key listener on a window to close it.
ActionListener closer = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        ((Component) c).setVisible(false);
};
JRootPane rootPane = c.getRootPane();
Toolkit toolkit = ((Component) c).getToolkit();
...
booleaninvoke(Action action, Object source)
invoke
if (action == null || !action.isEnabled()) {
    return false;
ActionEvent evt = new ActionEvent(source, ActionEvent.ACTION_PERFORMED,
        (String) action.getValue(Action.ACTION_COMMAND_KEY), 0);
action.actionPerformed(evt);
return true;
booleanisActionSelected(Action action)
is Action Selected
if (action == null)
    return false;
Boolean result = (Boolean) action.getValue(Action.SELECTED_KEY);
return result == null ? false : result;
booleanisKeyStrokeEvent(@Nullable final KeyStroke keyStroke, final int keyEventType, @Nullable final KeyEvent event)
is Key Stroke Event
boolean result = false;
if (keyStroke != null && event != null) {
    if (keyEventType == keyStroke.getKeyEventType()) {
        result = ((keyStroke.getModifiers() & event.getModifiers()) == keyStroke.getModifiers())
                && (keyStroke.getKeyCode() == event.getKeyCode());
return result;
...
booleanisValidKey(int keyCode)
Returns whether the specified key code is valid.
switch (keyCode) {
case KeyEvent.VK_ENTER:
case KeyEvent.VK_BACK_SPACE:
case KeyEvent.VK_TAB:
case KeyEvent.VK_CANCEL:
case KeyEvent.VK_CLEAR:
case KeyEvent.VK_SHIFT:
case KeyEvent.VK_CONTROL:
...