The currently selected menu or menu item in a JMenu or JPopupMenu is tracked by MenuSelectionManager and can be retrieved by calling MenuSelectionManager.getSelectedPath(). This method returns an array of MenuElement objects, representing all the menu objects that are part of the selected menu or menu item. For example, when a menu is opened in a menu bar, the sequence of elements in the path is: JMenuBar, JMenu, and JPopupMenu. If a menu item in the open menu is then selected, there will be fourth element, a JMenuItem.

The menu path also includes nested menus. For example, if the currently selected menu item is part of a nested menu in a menu bar, the sequence of elements is: JMenuBar, JMenu, JPopupMenu, JMenu, JPopupMenu, and JMenuItem. Note that a JMenu is always followed by a JPopupMenu.

If a menu item in a popup menu is selected, the sequence of menu objects is simply JPopupMenu and JMenuItem. If the menu item is part of a nest menu, the sequence is, JPopupMenu, JMenu, JPopupMenu, and JMenuItem.

    // Get the selected menu path
MenuElement[] path = MenuSelectionManager.defaultManager().getSelectedPath(); if (path.length == 0) {
// No menus are opened or menu items selected
} // Retrieve the labels of all the menu elements in the path
for (int i=0; i<path.length; i++) {
Component c = path[i].getComponent(); if (c instanceof JMenuItem) {
JMenuItem mi = (JMenuItem)c;
String label = mi.getText();
// Note: JMenu is a subclass of JMenuItem; also JMenuBar does not have a label
}
}
Related Examples

e813. 获得当前选择的菜单或菜单项的更多相关文章

  1. e815. 监听当前选择的的菜单或菜单项

    The currently selected menu or menu item in a JMenu or JPopupMenu is tracked by MenuSelectionManager ...

  2. 13、Java菜单条、菜单、菜单项

    13.Java菜单条.菜单.菜单项 一般用Java做界面时,都得牵涉到菜单条.菜单.菜单项的设计.菜单项放在菜单里,菜单放在菜单条里,且其字体均可设置. 13.1.菜单条(Menubar) Frame ...

  3. 在Unity3d编辑器中加入菜单以及菜单项

    在引用UZGUI插件时,u3d编辑器的菜单条发生了变化,新增了菜单和菜单项,于是乎自己也像尝试一下,看了EZGUI的About_EZ_GUI脚本文件后,结果大出我所料,原来SO EASY! using ...

  4. java学习:AWT组件和事件处理的笔记(1)--菜单条,菜单,菜单项

    菜单放在菜单条里,菜单项放在菜单里1.MenuBar    在java.awt包中,负责创建菜单条,即MenuBar的一个实例,便是一个菜单条.    在Frame类中的setMenuBar(Menu ...

  5. 为Windows资源管理器右键菜单添加菜单项

    为Windows资源管理器右键菜单添加菜单项 在Windows下命令行用的比较多,经常在资源管理器里翻到某个目录,若想要在此目录下跑命令,只能是打开cmd.exe,然后一路cd才能到达此目录. 每次都 ...

  6. e808. 建立菜单栏,菜单,菜单项

    When the user selects a menu item, it fires an action event. // Create the menu bar JMenuBar menuBar ...

  7. e814. 创建一个可监听选择状态的菜单项

    A menu item can receive notification of selection changes by overriding its menuSelectionChanged() m ...

  8. GUI tkinter (Menu)菜单项篇

    """添加顶层菜单:1.我们可以使用Menu类来新建一个菜单,Menu和其他的组件一样,第一个是parent,这里通常可以为窗口2.然后我们可以用add_command方 ...

  9. 在Winform界面菜单中实现动态增加【最近使用的文件】菜单项

    在我们一些和文件处理打交道的系统中,我们往往需要记录下最近使用的文件,这样方便用户快速打开之前浏览或者编辑过的文件,这种在很多软件上很常见,本文主要介绍在Winform界面菜单中实现[最近使用的文件] ...

随机推荐

  1. angular中的表单数据自定义验证

    之前说过了angular是如何给表单的数据进行基本的,常用的验证的:angular学习笔记(二十)-表单验证 但是在实际工作中,这些验证是远远不够的,很多时候我们需要自定义一些验证规则,以及一些异步, ...

  2. ActionScript 3操作XML 详解

    AS3引入了E4X ,它是根据ECMAScript标准处理XML 数据的全新机制.这使得程序员在程序中无缝地操作XML.在AS3中可以使用XML字面值将XML数据直接写入代码,该字面值将被自动解析. ...

  3. Hive中小表与大表关联(join)的性能分析【转】

    Hive中小表与大表关联(join)的性能分析 [转自:http://blog.sina.com.cn/s/blog_6ff05a2c01016j7n.html] 经常看到一些Hive优化的建议中说当 ...

  4. JAVA-数据库之Statement对象

    相关资料:<21天学通Java Web开发> 语句对象Statement1.语句对象Statement可以用来执行SQL语句,从而实现数据库操作.2.可以通过调用连接对象的createSt ...

  5. iOS微信支付demo运行报错解决如下

    要接入微信支付的小伙伴,首先要下载一份官方demo(APP微信支付官方Demo下载),然后打开工程,准备大干一场. 1.编译报错 编译的时候居然直接报错了(orz) 错误提示: APP微信支付官方De ...

  6. OCMock 3 参考

    Typorahtml, body {overflow-x: initial !important;}.CodeMirror { height: auto; } .CodeMirror-scroll { ...

  7. 【Unity笔记】提示框ToolTips大小自适应,及其闪烁的问题

    需求:制作了一个提示框,当鼠标移入背包格子内,显示提示框,且提示框位置跟随鼠标移动.当鼠标移出背包格子,隐藏提示框. 制作提示框ToolTips 因为提示框的大小要求随着显示的文本内容长度而自动大小适 ...

  8. C语言 · 征税程序

    算法提高 征税程序   时间限制:1.0s   内存限制:512.0MB      问题描述 税务局希望你帮他们编写一个征税程序,该程序的功能是:首先输入某公司的年销售额sale和税率rate,然后程 ...

  9. [phy]在uboot阶段失能phy芯片

    在uboot阶段power down掉phy芯片 uenvcmd=mdio 0x800; mmc rescan mdio为uboot支持命令 7:phy芯片地址 0:phy芯片工作模式寄存器地址 0x ...

  10. Linux:ssh端口转发详解

    ssh是个多用途的工具,不仅可以远程登录,还可以搭建socks代理.进行内网穿透,这是利用它的端口转发功能来实现的. 所谓ssh端口转发,就是在ssh连接的基础上,指定 ssh client 或 ss ...