Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Adding shortcut keys in java swing

  • 19-04-2004 2:52pm
    #1
    Closed Accounts Posts: 2,951 ✭✭✭


    Im trying to add some shortcut keys to some of my buttons in the menu of
    my Swing GUI. Apparantly its really easy, put in something like this and
    it should work

    JMenuItem m = new JMenuItem("Copy");
    m.setAccelerator(KeyStroke.getKeyStroke('C',
    Toolkit.getDefaultToolkit( ).getMenuShortcutKeyMask( ), false));

    When i try it on one of my menu items, like this

    private JMenuItem undoMenu = new JMenuItem("Undo");
    undoMenu.setAccelerator(KeyStroke.getKeyStroke('C',
    Toolkit.getDefaultToolkit( ).getMenuShortcutKeyMask( ), false));

    i get this error

    SimpleGUI.java:17: <identifier> expected

    undoMenu.setAccelerator(KeyStroke.getKeyStroke('C',Toolkit.getDefaultToo
    lkit( ).getMenuShortcutKeyMask( ), false));
    ^
    Anyone know what im doing wrong? Im importing javx.swing.*,
    java.awt.event.* and java.awt.*

    Thanks


Comments

  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    Been a while since I coded swing, but afair, there was a method for all JComponents called setMnemonic() which allowed you to create shortcut keys for things.

    I could be mistaken though.


  • Registered Users, Registered Users 2 Posts: 340 ✭✭DrEvil


    This is something I do know and have recent experience with so I have an answer for you. You are on the right track with setAccelerator method

    I find the best way is as follows -

    say i want Ctrl + C for jMenuItemX:

    jMenuItemX.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_C, ActionEvent.CTRL_MASK));


    in your case with an Undo menu item (Ctrl + Z)

    private JMenuItem undoMenu = new JMenuItem("Undo");
    undoMenu.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_Z, ActionEvent.CTRL_MASK));


    Hope that's of help to you :)


    Links to the JAVA 1.4.2 API (http://java.sun.com/j2se/1.4.2/docs/api) are as follows
    KeyStroke - http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/KeyStroke.html
    KeyEvent - http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/KeyEvent.html
    ActionEvent - http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/ActionEvent.html


  • Registered Users, Registered Users 2 Posts: 340 ✭✭DrEvil


    sry i forget to say about mnemonics

    If you wish to set a mnemonic on a menu item you would use the following code ( The mnemonic is the underlined letters in menu items. It's different to the setAccelerator)

    for the File menu you would use 'F'

    jMenuX.setMnemonic(KeyEvent.VK_F);



    setMnemonic(int) in the JAVA API

    setMnemonic

    public void setMnemonic(int mnemonic)

    Sets the keyboard mnemonic on the current model. The mnemonic is the key which when combined with the look and feel's mouseless modifier (usually Alt) will activate this button if focus is contained somewhere within this button's ancestor window.

    A mnemonic must correspond to a single key on the keyboard and should be specified using one of the VK_XXX keycodes defined in java.awt.event.KeyEvent. Mnemonics are case-insensitive, therefore a key event with the corresponding keycode would cause the button to be activated whether or not the Shift modifier was pressed.

    If the character defined by the mnemonic is found within the button's label string, the first occurrence of it will be underlined to indicate the mnemonic to the user.

    Parameters:
    mnemonic - the key code which represents the mnemonic



  • Closed Accounts Posts: 2,951 ✭✭✭L5


    Thanks for all the help, i got it working!


Advertisement