Java JMenu fillFontMenu(JMenu fontMenu)

Here you can find the source of fillFontMenu(JMenu fontMenu)

Description

fill Font Menu

License

Open Source License

Declaration

public static void fillFontMenu(JMenu fontMenu) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

public class Main {
    public static void fillFontMenu(JMenu fontMenu) {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Font[] fonts = ge.getAllFonts();

        List<String> names = new ArrayList<>();
        for (Font f : fonts) {
            String name = f.getFamily();
            if (!names.contains(name)) {
                addMenuItem(null, fontMenu, name);
                names.add(name);//w ww . j  a  va  2 s  . c o m
            }
        }

    }

    /**
     * add a new item to the give menu and add an action listener
     * 
     * @param li
     * @param menu
     * @param text
     * @return
     */
    public static JMenuItem addMenuItem(ActionListener li, JMenu menu, String text) {
        JMenuItem mi = new JMenuItem(text);
        mi.addActionListener(li);
        menu.add(mi);
        return mi;
    }

    /**
     * add a new item to the give menu, add an action listener and set the tool
     * tip
     * 
     * @param li
     * @param menu
     * @param text
     * @param tip
     * @return the new JMenuItem
     */
    public static JMenuItem addMenuItem(ActionListener li, JMenu menu, String text, String tip) {
        JMenuItem mi = new JMenuItem(text);
        mi.addActionListener(li);
        mi.setToolTipText(tip);
        menu.add(mi);
        return mi;
    }

    /**
     * add a new item to the give menu, add an action listener, set the tool tip
     * and set the keyboard accelerator
     * 
     * @param li
     * @param menu
     * @param text
     * @param tip
     * @param key
     * @return
     */
    public static JMenuItem addMenuItem(ActionListener li, JMenu menu, String text, String tip, String key) {
        JMenuItem mi = addMenuItem(li, menu, text, tip);
        mi.setAccelerator(KeyStroke.getKeyStroke(key));
        return mi;
    }
}

Related

  1. createMenuItemCustomAction(JMenu menu, int type, String text, String command, ImageIcon image, int acceleratorKey, String toolTip, ActionListener action)
  2. createMenuItems(JMenu menu, String[] menuItemNames)
  3. createTooltiplessJMenu(Action action)
  4. disableMenuIfAllMenuItemsAreDisabled(JMenu menu, boolean bRemoveEmptySubMenus)
  5. enableAll(JMenu menu)
  6. findMenuComponent(JMenu menu, String menuComponentName, Class componentClass)
  7. findMenuItem(JMenu menu, String menuItemName)
  8. findMenuItemByActionCommand(JMenu menu, String sActionCommand)
  9. findSubMenuWithLabel(JMenu menu, String text)