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. Java Base64 编码解码方案总结

    Base64是一种能将任意Binary资料用64种字元组合成字串的方法,而这个Binary资料和字串资料彼此之间是可以互相转换的,十分方便.在实际应用上,Base64除了能将Binary资料可视化之外 ...

  2. (原创)一个简洁通用的调用DLL函数的帮助类

    本次介绍一种调用dll函数的通用简洁的方法,消除了原来调用方式的重复与繁琐,使得我们调用dll函数的方式更加方便简洁.用过dll的人会发现c++中调用dll中的函数有点繁琐,调用过程是这样的:在加载d ...

  3. Spark SQL inferSchema实现原理探微(Python)【转】

    使用Spark SQL的基础是“注册”(Register)若干表,表的一个重要组成部分就是模式,Spark SQL提供两种选项供用户选择:   (1)applySchema     applySche ...

  4. 【驱动】linux设备驱动·入门

    linux设备驱动 驱动程序英文全称Device Driver,也称作设备驱动程序.驱动程序是用于计算机和外部设备通信的特殊程序,相当于软件和硬件的接口,通常只有操作系统能使用驱动程序. 在现代计算机 ...

  5. VMware文章总结

    Vmware Vsphere6.5 + Vcenter6.5安装简介:http://www.ctoclubs.com/?p=296 安装VCSA6.5(vCenter Server Appliance ...

  6. app测试初窥

    要用到的两个神器:abd&drozer adb介绍 adb的全称为Android Debug Bridge,就是起到调试桥的作用,作为一名开发者倒是常用到这个工具.借助adb工具,我们可以管理 ...

  7. [转]oracle 11g jdbc jar包在哪个文件目录

    oracle 11g jdbc jar包在哪个文件目录 一. 如果装了Oracle数据库的话, 大致是这样的目录:     D:\oracle\product\11.2.0\client_1\oui\ ...

  8. 火狐FireFox看视频不能全屏显示的问题

    问题:最大化/全屏时显示有问题,不能全屏显示,只能看到左上角一部分画面. 解决办法:在画面右键弹出的菜单中选择[设置]——勾选[启用硬件加速].

  9. 宇宙最强spacemacs

    这个标题背后的潜台词其实是:逼格是什么炼成的? 此处省略一万字. Emacs就不多说了,神之编辑器,但其快捷键实在是....Evil.好啦,现在来了Spacemacs,结合Vim与Emacs二者的优点 ...

  10. Go Revel - main函数分析

    运行revel命令时,首先会编译整个项目,在编译时,会根据`app.conf`配置文件生成两个源码文件`tmp/main.go`.`routes/routes.go`,其中`main.go`是整个项目 ...