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. [Windows Azure] Windows Azure Storage & SQL Database

    http://channel9.msdn.com/Series/Windows-Azure-Storage-SQL-Database-Tutorials Windows Azure offers mu ...

  2. SVN如何切换账号

    https://www.cnblogs.com/six-moon/p/5233878.html **************************************************** ...

  3. 【论文笔记】Training Very Deep Networks - Highway Networks

    目标: 怎么训练很深的神经网络 然而过深的神经网络会造成各种问题,梯度消失之类的,导致很难训练 作者利用了类似LSTM的方法,通过增加gate来控制transform前和transform后的数据的比 ...

  4. maven relativePath

    父项目的pom.xml文件的相对路径.默认值为../pom.xml.maven首先从当前构建项目开始查找父项目的pom文件,然后从本地仓库,最有从远程仓库.RelativePath允许你选择一个不同的 ...

  5. C# winform打包(带数据库安装)<转>

    使用VS自带的打包工具,制作winform安装项目 开发环境:VS2008 Access 操作系统:Windows XP 开发语言:C# 项目名称:**管理系统 步骤: 1.打开开发环境VS2010, ...

  6. [转]收藏的Extjs 多表头插件GroupHeaderGrid

    本文转载自chy2z<收藏的Extjs 多表头插件GroupHeaderGrid> 效果图: 是不是非常酷啊! js 代码: Ext.namespace("Ext.tet.plu ...

  7. regsvr32.exe是什么东西

    Regsvr32命令修复系统故障实例使用过activex的人都知道,activex不注册是不能够被系统识别和使用的,一般安装程序都会自动地把它所使用的activex控件注册,但如果你拿到的一个控件需要 ...

  8. swift闭包 notes http://www.gittielabs.com

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

  9. 多平台的网站实现单点登录系统(SSO)的开发思路 让你的会员中心更加统一(参考资料)

    单点登录并不是一个新鲜的玩意儿,比较官方的解释是企业业务整合的解决方案之一,通俗来讲SSO就是一个通用的用户中心,国内比较流行的UCenter就是一套单点登录解决方案.而近期以CSDN明文存储用户密码 ...

  10. Linux下用文件IO的方式操作GPIO(/sys/class/gpio)

    通过sysfs方式控制GPIO,先访问/sys/class/gpio目录,向export文件写入GPIO编号,使得该GPIO的操作接口从内核空间暴露到用户空间,GPIO的操作接口包括direction ...