Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 19, 2009, 07:48 PM   #1
Awesomeness
macrumors member
 
Join Date: Feb 2009
Java Menu Bar?

In a a java menu bar, the setMnemonic() method doesn't take a char anymore, it takes an int. They say this is better. WTF? How am I supposed to show a character with an int? Is it the octal character codes?
Awesomeness is offline   0 Reply With Quote
Old Mar 19, 2009, 10:47 PM   #2
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Seattle, WA
java.awt.event.KeyEvent has static int fields to pass to setMnemonic that corresponds to keys. If I read properly, casting an alphanumeric ASCII character to an int will be equivalent, but for others the members of KeyEvent should be used.

Docs are your friend. I'd post links, but posting from the phone. The javax.swing.AbstractButton doc describes how to use this.

-Lee
lee1210 is offline   0 Reply With Quote
Old Mar 19, 2009, 11:10 PM   #3
Awesomeness
Thread Starter
macrumors member
 
Join Date: Feb 2009
Yeah

Quote:
Originally Posted by lee1210 View Post
java.awt.event.KeyEvent has static int fields to pass to setMnemonic that corresponds to keys. If I read properly, casting an alphanumeric ASCII character to an int will be equivalent, but for others the members of KeyEvent should be used.

Docs are your friend. I'd post links, but posting from the phone. The javax.swing.AbstractButton doc describes how to use this.

-Lee
I always look at the docs. Usually they help... But they didn't this time. I'll try again.
Awesomeness is offline   0 Reply With Quote
Old Mar 19, 2009, 11:16 PM   #4
Awesomeness
Thread Starter
macrumors member
 
Join Date: Feb 2009
Quote:
Originally Posted by Awesomeness View Post
I always look at the docs. Usually they help... But they didn't this time. I'll try again.
Here's my code. I made it a while ago. I still doesn't work >.<

Code:
		open = new JMenuItem("Open Command...");
		open.setMnemonic(KeyEvent.VK_O);
		open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
			    Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
		fileMenu.add(open);
Awesomeness is offline   0 Reply With Quote
Old Mar 19, 2009, 11:25 PM   #5
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Seattle, WA
What happens? Does cmd+O do anything? What should happen when the menu item is selected? Maybe adding the additional code that deals with O is mucking things up?

-Lee
lee1210 is offline   0 Reply With Quote
Old Mar 19, 2009, 11:57 PM   #6
Awesomeness
Thread Starter
macrumors member
 
Join Date: Feb 2009
Quote:
Originally Posted by lee1210 View Post
What happens? Does cmd+O do anything? What should happen when the menu item is selected? Maybe adding the additional code that deals with O is mucking things up?

-Lee
As of now, there is no action listener. Even with one I still don't get any sign of command O doing anything. Even without an action listener it should just flicker blue when I press command O, but not do anything. There is nothing else which could mess it up. I tried removing the additional code. Nothing. I got the additional code and thought it did what it was supposed to do. I got the code I thought was going to do it from http://developer.apple.com/documenta...tegration.html
The only progress I've made is making it show the command symbol (⌘) on the menu.

Last edited by Awesomeness; Mar 20, 2009 at 12:00 AM. Reason: update
Awesomeness is offline   0 Reply With Quote
Old Mar 20, 2009, 12:28 AM   #7
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Seattle, WA
For kicks you might check to see if:
KeyEvent.VK_O == (int)'O'
Or
KeyEvent.VK_O == (int)'o'

That's more just for your own edification. You might also try passing (int)'O' to setMnemonic to see if that works.

-Lee

Edit: after reading the link you posted, it looks like setMnemonic is discouraged, and just the setAccelerator should do. If the item does nothing, how do you know if it was "pressed" or not?

Last edited by lee1210; Mar 20, 2009 at 12:39 AM.
lee1210 is offline   0 Reply With Quote
Old Mar 20, 2009, 01:55 AM   #8
Awesomeness
Thread Starter
macrumors member
 
Join Date: Feb 2009
Yes!

Quote:
Originally Posted by lee1210 View Post
For kicks you might check to see if:
KeyEvent.VK_O == (int)'O'
Or
KeyEvent.VK_O == (int)'o'

That's more just for your own edification. You might also try passing (int)'O' to setMnemonic to see if that works.

-Lee

Edit: after reading the link you posted, it looks like setMnemonic is discouraged, and just the setAccelerator should do. If the item does nothing, how do you know if it was "pressed" or not?
I got it! The correct code snippet is:
Code:
		//Create the menu bar.
		menuBar = new JMenuBar();
		int mask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();

		//Build the first menu.
		fileMenu = new JMenu("File");
		fileMenu.getAccessibleContext().setAccessibleDescription("File");
		menuBar.add(fileMenu);

		//a group of JMenuItems
		open = new JMenuItem("Open Command...");
		open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, mask));
		open.addActionListener(this);
		fileMenu.add(open);
		fileMenu.addSeparator();
Yeah... It works! I tried it with an ActionListener! The top bar doesn't flicker blue when you use the hotkey though... You'd have to click on it to make it do that.
Awesomeness is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
User Name not Appearing in Top Menu Bar applefan27073 OS X 7 Nov 9, 2011 02:40 AM
Account name in the menu bar danakm Mac OS X 10.7 Lion 9 Jul 21, 2011 07:39 AM
Apple Menu Bar: Mouse-over auto hide and show zdlyons OS X 8 Feb 27, 2011 01:51 PM
How can I stop mac menu bar appearing in games polo3400 Mac and PC Games 4 Feb 17, 2011 04:31 PM
Screen resolution problems results in double menu bar frannyy MacBook 0 Nov 6, 2010 09:03 AM


All times are GMT -5. The time now is 05:06 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

Privacy / DMCA contact / Affiliate and FTC Disclosure
Copyright 2002-2013, MacRumors.com, LLC