Java JMenuBar addFastKeys(JMenuBar menuBar)

Here you can find the source of addFastKeys(JMenuBar menuBar)

Description

add Fast Keys

License

Open Source License

Declaration

public static void addFastKeys(JMenuBar menuBar) 

Method Source Code


//package com.java2s;
// GNU General Public License:                                                //

import java.awt.*;

import javax.swing.*;

public class Main {
    public static void addFastKeys(JMenuBar menuBar) {
        for (Component menuComponent : menuBar.getComponents()) { //iterate over menus
            JMenu menu = (JMenu) menuComponent;
            menu.setMnemonic(menu.getText().charAt(menu.getText().indexOf('&') + 1));
            menu.setText(menu.getText().replace("&", ""));
            for (Component menuItemComponent : menu.getMenuComponents())
                if (menuItemComponent instanceof JMenuItem) { //skip separators
                    JMenuItem menuItem = (JMenuItem) menuItemComponent;
                    menuItem.setMnemonic(menuItem.getText().charAt(menuItem.getText().indexOf('&') + 1));
                    menuItem.setText(menuItem.getText().replace("&", ""));
                }/*from w  w  w  .j  a  va2 s. c  o  m*/
            /*
            for (int count = 0; count < menu.getMenuComponentCount(); count++) {  //iterate over menu items
               Component menuItemComponent = menu.getMenuComponent(count);
               if (menuItemComponent instanceof JMenuItem) {   //skip separators
              JMenuItem menuItem = (JMenuItem)menuItemComponent;
              menuItem.setMnemonic(menuItem.getText().charAt(
             menuItem.getText().indexOf('&') + 1));
              menuItem.setText(menuItem.getText().replace("&", ""));
              }
               }
            */
        }
    }

    public static void addFastKeys(JButton[] buttonList) {
        for (JButton button : buttonList) {
            button.setMnemonic(button.getText().charAt(button.getText().indexOf('&') + 1));
            button.setText(button.getText().replace("&", ""));
        }
    }
}

Related

  1. addMenuItems(JMenuBar menu, JComponent... items)
  2. applyDefaultProperties(final JMenuBar comp)
  3. cloneMenuBar(final JMenuBar menubar)
  4. createMenuInMenuBar(final JMenuBar menuBar, final String menuName)