KeyStroke madness

  • Thread starter =?ISO-8859-1?Q?St=E9phane_St-Pierre?=
  • Start date
?

=?ISO-8859-1?Q?St=E9phane_St-Pierre?=

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);
}
}
 
G

Gerbrand van Dieijen

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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top