KeyStroke madness

Discussion in 'Java' started by =?ISO-8859-1?Q?St=E9phane_St-Pierre?=, Sep 19, 2003.

  1. HI, I'd like to create a Class that would give me JMenuItem generated
    with specific
    parameters,like the name and - specially, the Key Accelerator. My
    problem is that I just can't pass
    any int value to the getKeyStroke(ke, ev) method, event with the
    appropriate import. How can I use the VK_C "constant" and pass it to
    the method ? I figure a sad one : (int)'C' but it ain't secure and
    it's ugly.

    What I want to do is to call my constructor with :
    new JSMenu(menuEdition, KeyEvent.VK_C, META_MASK);



    import javax.swing.event.MenuKeyEvent.*;
    import java.awt.event.KeyEvent.*;


    final JSMenu smenuCut = new JSMenu(menuEdition, "Cut", (int)'C', 4);


    //================================================================================
    //class JSousMenu
    final class JSMenu extends JMenuItem
    {
    public JSMenu(JMenu menu, String sMenu, int ke, int ev)
    {
    super(sMenu);

    this.setAccelerator(KeyStroke.getKeyStroke(ke, ev));
    this.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    System.exit(0);
    }});

    menu.add(this);
    }
    }
     
    =?ISO-8859-1?Q?St=E9phane_St-Pierre?=, Sep 19, 2003
    #1
    1. Advertising

  2. On 19 Sep 2003 14:47:10 -0700, St├ęphane St-Pierre wrote:
    >HI, I'd like to create a Class that would give me JMenuItem generated
    >with specific
    >parameters,like the name and - specially, the Key Accelerator. My
    >problem is that I just can't pass
    >any int value to the getKeyStroke(ke, ev) method, event with the
    >appropriate import. How can I use the VK_C "constant" and pass it to
    >the method ? I figure a sad one : (int)'C' but it ain't secure and
    >it's ugly.
    >


    You can use the Action and AbstractAction and KeyStroke object for that.

    It's slightly more complicated to use, but supports all things to
    programmatically create menu files from (for example) resource files.

    I've created a class with methods that do similar things like what you're
    writing, if your interested I can mail it (free of course).

    --
    Gerbrand van Dieijen

    WWW: http://twisted.warande.net
    ICQ: 19345450
     
    Gerbrand van Dieijen, Sep 20, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JustJohn
    Replies:
    6
    Views:
    645
    JustJohn
    Mar 29, 2006
  2. Herb Stull

    Keystroke Count

    Herb Stull, Oct 17, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    3,362
    Will T
    Oct 22, 2003
  3. Fred
    Replies:
    6
    Views:
    3,409
    pete kirkham
    Aug 2, 2003
  4. Brian Simpson

    Capture keystroke

    Brian Simpson, Jan 22, 2004, in forum: Java
    Replies:
    1
    Views:
    1,021
    Gordon Beaton
    Jan 22, 2004
  5. Marius Horak

    Madness, I call it madness

    Marius Horak, Jun 4, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    161
    Raterus
    Jun 4, 2004
Loading...

Share This Page