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. Capterra Software Categories

    https://www.capterra.com/categories this software categories is valuable.

  2. C++11 constexpr使用

    C++11为了提高代码执行效率做了一些改善.这种改善之一就是:生成常量表达式,允许程序利用编译时的计算能力.假如你熟悉模板元编程,你将发现constexpr使这一切变得更加简单.constexpr使我 ...

  3. sql2005分页储存过程和C#分页类

    1.sql2005分页储存过程 --sql2005,qq524365501 create Procedure up_page2005 ), -- 表名称 ), -- 列 ), -- 主键,用于统计总数 ...

  4. #define SIG_DFL ((void(*)(int))0)

    在linux内的信号处理函数中,有#define SIG_DFL ((void(*)(int))0)和#define SIG_IGN ((void(*)(int))1)两个宏定义.要理解这两个宏定义, ...

  5. vmware的硬件选项里有关于虚拟化引擎的选项(虚拟机支持硬件虚拟化)

    前一阵子在使用陈沙克老师的博客安装devstack的时候就有一个疑问: 使用vmware创建虚拟机时,硬件选项里有关于虚拟化引擎的选项到底都代表了什么意思?  Intel VT-x/EPT和AMD-V ...

  6. Spring Batch并行与扩展

    1. 概述 Spring Batch提供了多种方式用于处理并行,提高性能.主要分为2大类: - 单个进程,多线程 - 多个进程 因此,可以细分为以下几类: - 多线程Step(Multi-thread ...

  7. ADO.NET 实体数据模型 异常-“序列化类型为 XX 的对象时检测到循环引用”

    发生异常的代码如下: 1: public JsonResult GetSaleByNo1(string id) 2: { 3: SaleMvcUI.Helper.saleDBEntities sale ...

  8. PHP error_reporting() 函数

    实例 规定不同的错误级别报告: <?php // 关闭错误报告 error_reporting(0); // 报告 runtime 错误 error_reporting(E_ERROR | E_ ...

  9. bash里wget失败

    直接使用wget是可以的,然而在shell脚本里却不行,后来发现原来是换行符的问题,编辑器默认的是\r\n,一不留神,自己把自己坑了

  10. python 入门练习

    1.猜拳游戏 import randomimport sys #from random import randintdef guess(): ubuntu = random.randint(0,2)# ...