zoukankan      html  css  js  c++  java
  • e813. 获得当前选择的菜单或菜单项

    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
  • 相关阅读:
    Rancher 2.1平台搭建及使用
    回归博客园
    CGI与FastCGI
    [转]1小时内打造你自己的PHP MVC框架
    MySQL学习随笔--通过实例理解merge ,temptable算法的差异
    MySQL学习随笔--视图
    使用onenote写博客园的方法
    手动配置wamp环境(1)--apache安装与基本操作
    文档兼容性定义,使ie按指定的版本解析
    JavaScript线程
  • 原文地址:https://www.cnblogs.com/borter/p/9596172.html
Copyright © 2011-2022 走看看