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. linux下配置某程序的sudo不用输密码

    $ su密码: # cd /etc/# cp sudoers sudoers_bak# vi sudoers 最下面加入一行:ALL ALL = NOPASSWD:/usr/sbin/openconn ...

  2. frame自适应

    <html> <head> <title>frame自适应</title> </head> <frameset rows=" ...

  3. js 数组的增删改查

    js数组元素的添加和删除一直比较迷惑,今天终于找到详细说明的资料了,先给个我测试的代码^-^ var arr = new Array(); arr[0] = "aaa"; arr[ ...

  4. export default与export的区别

    1.export default 和export都可以用于导出常量,函数,文件,模块等: 2.可以在模块中通过import+(常量 | 函数 | 文件 | 模块)名的方式,将其导入,以便能够对其进行使 ...

  5. LeetCode: Search Insert Position 解题报告

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  6. python 字符串编码 str和unicode 区别以及相互转化 decode('utf-8') encode('utf-8')

  7. 通过python-libvirt管理KVM虚拟机 代码实现

    初步代码 <span style="font-size:18px;">''''' Work with virtual machines managed by libvi ...

  8. 有了#ifdef 为什么还需要#if defined

    有了#ifdef 为什么还需要#if  defined ? #include <stdio.h> #define A #define B void test(int a,int b) { ...

  9. S3C2440串口的基本使用

    2440A有三个串口,我们使用串口0对它进行了解熟悉. 首先肯定是应该找到手册上串口0所对应的引脚,然后配置相应寄存器. 串口0对应GPIO H的 2,3 串口在单片机中我们已经有很多使用经验了,对于 ...

  10. [转]mybatis如何直接 执行传入的任意sql语句 并按照顺序取出查询的结果集

    原文地址:https://www.cnblogs.com/wuyun-blog/p/5769096.html 需求: 1.直接执行前端传来的任何sql语句,parameterType="St ...