Results 1 to 6 of 6
  1. #1
    rpwtdj's Avatar
    rpwtdj is offline Member
    Join Date
    Dec 2007
    Posts
    15
    Rep Power
    0

    Question How to add hotkey(Ctrl+VK) to a button?

    Hi buddies,
    I've added a button to a panel, and used
    Java Code:
    setMnemonic(KeyEvent.VK_R)
    to set a hotkey Alt+R for triggering the button event.
    However, the requirement is changed, now, I need to use 'Ctrl+R' as the
    hotkey for the button.
    Will you please give me some hit on this?
    Thanks!

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Test {
        public static void main(String[] args) {
            JButton button = new JButton();
            button.setAction(new AbstractAction("press me") {
                {
                    putValue(Action.ACTION_COMMAND_KEY, getValue(Action.NAME));
                }
                public void actionPerformed(ActionEvent e) {
                    System.out.println(e.getActionCommand());
                }
            });
            JPanel panel = new JPanel();
            panel.add(button);
            panel.setPreferredSize(new Dimension(200,100));
            // Bind a keystroke to the button to act as accelerator.
            int c = JComponent.WHEN_IN_FOCUSED_WINDOW;
            KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_R,
                                        InputEvent.CTRL_DOWN_MASK);
            panel.getInputMap(c).put(ks, "PRESS");
            panel.getActionMap().put("PRESS", button.getAction());
            JOptionPane.showMessageDialog(null, panel,"", -1);
        }
    }

  3. #3
    rpwtdj's Avatar
    rpwtdj is offline Member
    Join Date
    Dec 2007
    Posts
    15
    Rep Power
    0

    Default

    Thank you hardwired.
    Great help.

  4. #4
    rpwtdj's Avatar
    rpwtdj is offline Member
    Join Date
    Dec 2007
    Posts
    15
    Rep Power
    0

    Default

    How can I make the 'r' in "press me" underlined?
    so that the button will look like "press me"

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Swing labels support primitive html 3.2:
    Java Code:
            String s = "<html>p<u>r</u>ess me";
            button.setAction(new AbstractAction(s) {
                {
                    putValue(Action.ACTION_COMMAND_KEY, "press me");
                }

  6. #6
    rpwtdj's Avatar
    rpwtdj is offline Member
    Join Date
    Dec 2007
    Posts
    15
    Rep Power
    0

Similar Threads

  1. Open type (Ctrl-Shift-T) does not find types
    By Zhenya_Merom in forum Eclipse
    Replies: 2
    Last Post: 11-04-2009, 10:15 AM
  2. Run method on Ctrl+Shift+D
    By Echilon in forum AWT / Swing
    Replies: 1
    Last Post: 02-16-2008, 10:31 PM
  3. Using SWT Button
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 09:05 AM
  4. how to issue the command of Ctrl-C (copy) in Java
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-18-2007, 03:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •