java - JMenuItem accelerator not working when menu bar is hidden -
this question follow-up this question.
i have jmenubar
behavior menu bars in firefox , itunes. is, menu bar hidden, when press alt
, menu bar appears.
the answer other question solved problem of achieving functionality, brought another issue: jmenuitem
accelerators not working when jmenubar
not visible. in other words, must press alt
before ctrl+f
(the installed accelerator) work.
this not supposed case, though, because setaccelerator()
method states following:
public void setaccelerator(keystroke keystroke)
sets key combination invokes menu item's action listeners without navigating menu hierarchy. ui's responsibility install correct action. note when keyboard accelerator typed, work whether or not menu displayed.
so, i'm wondering if java bug?
sscce (to menu appear, press alt
, , installed accelerator ctrl+f
"find" brings dummy joptionpane
input):
import javax.swing.jframe; import javax.swing.jmenu; import javax.swing.jmenubar; import javax.swing.jmenuitem; import javax.swing.joptionpane; import javax.swing.keystroke; import javax.swing.menuelement; import javax.swing.menuselectionmanager; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.windowconstants; import javax.swing.event.changeevent; import javax.swing.event.changelistener; public class menubartest extends jframe { public menubartest() { jmenu jmenu1 = new jmenu(); jmenu jmenu2 = new jmenu(); final jmenubar jmenubar1 = new jmenubar(); jmenuitem jmenuitem1 = new jmenuitem(); jmenuitem jmenuitem2 = new jmenuitem(); setdefaultcloseoperation(windowconstants.exit_on_close); jmenu1.settext("file"); jmenuitem1.settext("jmenuitem1"); jmenu1.add(jmenuitem1); jmenubar1.add(jmenu1); jmenu2.settext("edit"); jmenuitem2.settext("find"); jmenu2.add(jmenuitem2); jmenubar1.add(jmenu2); setjmenubar(jmenubar1); jmenubar1.setvisible(false); changelistener listener = new changelistener() { @override public void statechanged(changeevent e) { menuelement[] elements = menuselectionmanager.defaultmanager().getselectedpath(); jmenubar1.setvisible(elements.length > 0 && elements[0] == jmenubar1); } }; menuselectionmanager.defaultmanager().addchangelistener(listener); jmenuitem2.setaccelerator(keystroke.getkeystroke(java.awt.event.keyevent.vk_f, java.awt.event.inputevent.ctrl_mask)); jmenuitem2.settext("find"); jmenuitem2.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { string = joptionpane.showinputdialog(menubartest.this, "search what?"); system.out.println(what); } }); pack(); setsize(500,500); setlocationrelativeto(null); setvisible(true); } public static void main(string args[]) { try { uimanager.setlookandfeel("com.sun.java.swing.plaf.windows.windowslookandfeel"); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { ex.printstacktrace(); } java.awt.eventqueue.invokelater(new runnable() { public void run() { new menubartest(); } }); } }
read emphasis carefully
note when keyboard accelerator typed, work whether or not menu displayed.
this talks menu not parent. meaning menu might not displayed. nevertheless, real (probably not overly documented) desisive property must showing. had updated answer previous question.
Comments
Post a Comment