Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortcut key for jButton without using alt key

Tags:

java

swing

In SWT you can give any button a shortcut key simply by adding & in front of the letter in the button label. For example, if my button label is &Play, I can activate the button by hitting letter p on the keyboard.

In Swing, you can add a shortcut key using the mnemonic property. However, you need to hit alt+p to activate the button. This is really most appropriate for menu shortcuts. I want to activate the button with a letter press and no alt modifier.

I've seen this post on how to do it, but it seems absurdly complicated. Is there an easier way to do this?

http://linuxjavaprogrammer.blogspot.com/2008/01/java-swing-jbutton-keyboard-shortcuts.html

Update: After @camickr suggestion, I ended up using this code. I couldn't find any clear and simple example online, so hopefully this will help people out.

// play is a jButton but can be any component in the window
play.getInputMap(play.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_P, 0), "play");
play.getActionMap().put("play", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
  playActionPerformed(e);  // some function
}
});
like image 800
Rich C Avatar asked Nov 21 '10 22:11

Rich C


People also ask

How do I see all keyboard shortcuts?

Press Ctrl + Alt + ? on your keyboard.

How do I add a shortcut to JMenuItem?

You can add a KeyBinding to your JMenuItem like this: Action sayHello = new AbstractAction() { public void actionPerformed(ActionEvent e) { JOptionPane. showMessageDialog(null,"Hello World, From JMenuItem :)"); } }; jMenuItem. getInputMap(JComponent.

What is the shortcut key U?

Alternatively referred to as Control+U and C-u, ^u, Ctrl+U is a keyboard shortcut often used to underline text. On Apple computers, the shortcut for underline Command + U . How to use the Ctrl+U keyboard shortcut. Ctrl+U in an Internet browser.


2 Answers

Yes, Swing was designed to use Key Bindings. So instead of adding an ActionListener to the button you add an Action. Then that Action can be shared by the button or a menu item. You can also assign any number of KeyStrokes to invoke the Action by using the KeyBindings. The tutorial also has a section on Actions which explains why using an Action is beneficial.

JComponent has a registerKeyboardAction(...) method which basically does the InputMap/ActionMap bindings for you, but it also has to wrap the ActionListener in a wrapper Action so its preferrable for you to do you own bindings.

like image 96
camickr Avatar answered Oct 10 '22 00:10

camickr


Further to camickr's answer, I am now using a little utility function like this:

public static void clickOnKey(
    final AbstractButton button, String actionName, int key )
{
    button.getInputMap( JButton.WHEN_IN_FOCUSED_WINDOW )
        .put( KeyStroke.getKeyStroke( key, 0 ), actionName );

    button.getActionMap().put( actionName, new AbstractAction()
    {
        @Override
        public void actionPerformed( ActionEvent e )
        {
            button.doClick();
        }
    } );
}

Now to set the keyboard shortcut for a button I just do:

clickOnKey( playButton, "play", KeyEvent.VK_P );
like image 43
Andy Balaam Avatar answered Oct 10 '22 00:10

Andy Balaam