Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / November 2003

Tip: Looking for answers? Try searching our database.

Applet JMenuItem Mnemonic + Accelerator Keys dont function

Thread view: 
Marcel van Gellekom - 28 Nov 2003 23:16 GMT
hi,

im starting new with applets and with my first menu, i seem i cannot
get the menu keys working.
i declare my Applet class "extends Applet implements ActionListener
{.." as i did with my former JFrame class, create my menu as usual and
implement "public void actionPerformed(ActionEvent e) {..".
but no accelerator key seems to work not even the menu pops up so the
mnemonic keys don't work either. i have the bad feeling i have
misunderstood some general difference between an applet window and a
jframe windows .. or i'm missing some other important grasp on
applets.
can anybody help me out plz :).

thx & greetz

marcel
Luc Van Bogaert - 30 Nov 2003 08:43 GMT
> i declare my Applet class "extends Applet implements ActionListener
> {.." as i did with my former JFrame class, create my menu as usual and
> implement "public void actionPerformed(ActionEvent e) {..".
> but no accelerator key seems to work not even the menu pops up so the
> mnemonic keys don't work either.

When you construct the JMenuOption objects, remember to use
addActionListener(this) on them to register your applet class as the
actionlistener for these menu options.

Signature

Luc Van Bogaert

Marcel van Gellekom - 30 Nov 2003 10:05 GMT
> > i declare my Applet class "extends Applet implements ActionListener
> > {.." as i did with my former JFrame class, create my menu as usual and
[quoted text clipped - 5 lines]
> addActionListener(this) on them to register your applet class as the
> actionlistener for these menu options.

i did it, my code for one item for example looks like that:
JMenuItem menuItem = new JMenuItem("Cancel");

menuItem.setMnemonic(KeyEvent.VK_C);

menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
ActionEvent.ALT_MASK));

menuItem.setActionCommand("cancel");

menuItem.addActionListener(this);

menu.add(menuItem);

thank you for the answer anyway! maybe there is something else i missed...
Luc Van Bogaert - 30 Nov 2003 12:47 GMT
> JMenuItem menuItem = new JMenuItem("Cancel");
> menuItem.setMnemonic(KeyEvent.VK_C);
[quoted text clipped - 5 lines]
>
> thank you for the answer anyway! maybe there is something else i missed...

Given this example code, what does your actionPerformed() method look like?

It should test somewhere for the source of the ActionEvent object, like :

if (e.getActionCommand().equals("cancel")) {
    ...
}

Signature

Luc Van Bogaert

Marcel van Gellekom - 30 Nov 2003 13:39 GMT
> Given this example code, what does your actionPerformed() method look like?
>
[quoted text clipped - 3 lines]
> ...
> }

yes it does indeed:

public void actionPerformed(ActionEvent e) {
if ("cancel".equals(e.getActionCommand())) {

   // cancel action

}

}

anything else? maybe the eclipse appletviewer cant handle this right - i
will try it out on a html page


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.