Java Swing JMenu Mnemonic - java

I have a menu that I created using JMenu. I want to assign a shortcut key Alt-F to this menu. I used setMnemonic('F') to do that, but the menu does not recognise the mnemonic.
What is the best way to troubleshoot or debug this problem ? I find that setting a break point does not help that much.
Thank you.
Code Snippet:
//higher up in variable declaration
/** Menus on the menu bar */
private JMenu uiFindMnu = new JMenu("Find");
...
//inside the constructor
// set mnemonic for the Find menu
uiFindMnu.setMnemonic('F');

Inside of a class constructor (extending JFrame):
JMenu helpmenu = new JMenu("File");
helpmenu.setMnemonic('F');
JMenuBar menubar = new JMenuBar();
menubar.add(helpmenu);
setJMenuBar(menubar);
That worked fine for me. You'll have to give some more details about your code in order for me to give a better answer. As far as troubleshooting SWING or any application GUI, one of the best recommendations I can give is to create the simplest possible scenario. I keep a bare-bones JFrame template around that I can throw simple code like this inside for testing. Once you know it works in the simplest scenario you can step back to your project and discover what other portion of your GUI is causing a conflict with this functionality.
Just out of curiosity, you don't happen to have a local variable called 'uiFindMnu' in your Constructor that is hiding your class variable, do you? I'd double check to make sure that the variable that you are calling setMnemonic() on is the one that is added to your MenuBar (and actually displayed).

Suffered with similar problem and realised that due to the setting of Look and feel after initialising the components caused the issue. Flipped the statements and it worked.
Posted a blog post here

Use setMnemonic(KeyEvent.VK_F);
I recommend you to read this about JMenus : Howto use Menus
Here is an extract of this article :
Menus support two kinds of keyboard alternatives: mnemonics and
accelerators. Mnemonics offer a way to use the keyboard to navigate
the menu hierarchy, increasing the accessibility of programs.
Accelerators, on the other hand, offer keyboard shortcuts to bypass
navigating the menu hierarchy. Mnemonics are for all users;
accelerators are for power users.
A mnemonic is a key that makes an already visible menu item be chosen.
For example, in MenuDemo the first menu has the mnemonic A, and its
second menu item has the mnemonic B. This means that, when you run
MenuDemo with the Java look and feel, pressing the Alt and A keys
makes the first menu appear. While the first menu is visible, pressing
the B key (with or without Alt) makes the second menu item be chosen.
A menu item generally displays its mnemonic by underlining the first
occurrence of the mnemonic character in the menu item's text, as the
following snapshot shows. B is the mnemonic character for this menu
item
You can specify a mnemonic either when constructing the menu item or
with the setMnemonic method. Here are examples of setting mnemonics
and accelerators:
//Setting the mnemonic when constructing a menu item:
menuItem = new JMenuItem("A text-only menu item",
KeyEvent.VK_T);
//Setting the mnemonic after creation time:
menuItem.setMnemonic(KeyEvent.VK_T);
As you can see, you set a mnemonic by specifying the KeyEvent constant
corresponding to the key the user should press.

Related

How to add custom menu action and titlebar icon to JFrame

I have a custom JFrame. On the title bar I have an icon in the top left, a title, and then the standard minimize, maximize, and close buttons on the right.
When I click the icon in the title bar I get the standard options: Restore, Move, Size, Minimize, Maximize, Close.
How can I add my own menu option here? I'd like to add and "Always On Top" option here.
Additionally I'd like to add a button next to the max,min,close buttons on the title bar to allow uses to toggle the "Always On Top" state of the JFrame.
You could create your own customized Components. To do that, create a new class which extends JMenuBar for example and override the methods which fit your needs. Very often, for example, one wants to override paintComponent(Graphics).
That is not what you want, 'though. Customizing the JMenuBar wont work as you expect it to. The "JMenuBar" is another bar below the title bar. I am Mac user, but as far as my knowledge goes, it is not possible to customize the title bar, because that isn't handled by the JVM. The only thing that is modifiable without using native code is the Icon in the top left.
For further information on that, look at this question and the best answer there. This will help you a lot.
Your problem (adding a button at the top for toggling the alwaysOnTop status) is best solved by creating normal instances of a JMenuBar, a JMenu and a JMenuItem.
To then add that MenuBar to your Frame, use JFrame.setJMenuBar(JMenuBar). See also How to use Menus.
I hope this helps!

Where in Java Swing Source is the implementation of the Enter Keypress on a menu?

I am mucking around with a hierarchical menu trying to make it scrollable. Yes, I know about Menu Scroller at the Java Tips Weblog, but it doesn't quite do what I want, so I've been mucking about with a stripped down version of it it and I'm not quite getting it to work.
Basically I want a JMenu with too many items to display on which the user can press the up and down arrow keys to scroll the menu. I have gotten tanatalizingly close to what I want but I have come to a hurdle which I can best describe this way:
When [ENTER] is pressed while a popup menu has focus, default behavior is to do the action associated with the selected item and dispose of the menu. If the menu is nested, popups above it in the hierarchy also close (become invisible). Where is this behavior coded? I've looked all over JMenu, JPopupMenu, JMenuItem, AbstractButton and I don't see what I am looking for. Where is the Swing source code that executes this common behavior?
If I knew the answer to that, I might understand why my implementation isn't working. I can do the action, but the menu and its parents won't disappear. I can make the menu disappear by setVisible(false) of course, but I can't walk the containment hierarchy to find the parent menus and make THEM disappear.
I can do the action, but the menu and its parents won't disappear.
I think you can use:
MenuSelectionManager.defaultManager().clearSelectedPath()
I'm not 100% certain for menus, but I know for JTextComponents that all of the keystrokes (copy, paste, enter, move forward by words/sentences/lines, deleting, etc.) are implemented via the InputMap and ActionMap. JTextcomponents also use Keymaps, but I'm pretty sure those are specific to text components.

Leave Swing JMenuBar tree open after selecting certain elements

I am currently in the process of self-teaching myself Java through the very comprehensive and readable text by Horstmann and Cornell published by Sun (8th ed/Vol 1. ISBN: 978-0-12-235476-9) and through completing one of the Swing examples (Listing 9-8) I noticed an annoying action performed upon selecting "Toggle" menu items.
The example shows toggling between certain options using the JCheckBoxMenuItem and JRadioButtonMenuItem classes. I noticed that upon selection of one of those menu components, the entire tree traversed closes. Is there a way to stop this menu closing through either a settable property of the items, or a method called in the ActionListener provided?
Link to authors code dump: Here
Cheers for any response. Would be a nice tweak to chuck into UI implementation further down the line.
not possible from Java6, for JPopup used for JMenu and JComboBox, popup is hidden from mouse a keyboar events
could be possible for custom popup for JMenu/JComboBox based on (undecorated) JDialog or JWindow, with JButtons (in your case with JCheckBox/JRadioButtons) layed by GridLayout

Open popupMenu only on mouse-click Swing

I have a question related with JPopupMenu that maybe someone can help me.
As far as I can see, a JPopupMenu opens when you press a JMenu (is this is in a JMenuBar) or when you hover over a JMenu (if this is in a JPopupMenu).
So, I have a JMenuBar containing a JMenu (let's call it 'File'). When I press 'File', a JPopupMenu open, which contains a JMenuItem and another JMenu (let's call it 'Properties').
Is there any way I can apply the same behaviour to the Properties JMenu as I have in the
File JMenu: only opens the JPopup when the user click it and not hover over it?
I know this is trying to avoid the normal behavior for the JMenu, but any of you have an idea if this is possible?
This is something very specific and I dont know if there are many people doing this. :)
Anyway, I leave a solution for this in case someone needs it.
First is important to notice that these changes are done in the UI classes. Both BasicMenuItemUI and BasicMenuUI have a inner class Handler that take care about the mouse events. The inner class Handler in the BasicMenuUI extends the inner class Handler in the BasicMenuItemUI and because they private and anonymous (respectively) is hard to change the code.
But, in both cases, there are a protected Handler which points to the Handlers I talked in the previous paragraph.
So, as a solution I extended those protected handlers in my own MyOwnBasicMenuUI and MyOwnBasicMenuItemUI implementations and I applied my own behavior.
Now I have a JMenu which only opens when the user clicks it, and remains open till the user clicks somewhere else.
If someone wants more details, I can provide them.
Thanks

Showing popup menu to menu item

My problem is this
I want to show a popup menu to a menu item in java, this i can easily achieve,
but when i am showing the popup menu the parent menu clears,
behaviour i want is parent menu also should be visible while showing popup menu
May I first ask "why"? It seems like very uncommon, thus bad usability. There's a good reason why you don't see this in any main stream application. If you want submenu, use submenu like trashgod's sample.
You can add a PopupMenuListener to your parent menu's popup menu, break at popupMenuWillBecomeInvisible and see the call stack. In JDK6, It comes from BasicMenuItemUI#doClick calling MenuSelectionManager#clearSelectedPath.
So if you really have a good reason to surprise user, supply your own menu item UI.
I'm looking at two demos, PopupMenuDemo and GraphPanel, and I don't see any clearing. PopupMenuDemo shows a hierarchical submenu, while GraphPanel shows a hierarchical context menu. Do you have an example in which "the parent menu clears."
Addendum: Here's what I see for PopupMenuDemo. Neither demo appears to invoke setVisible() on the menu.

Categories