Here you can find the source of fillFontMenu(JMenu fontMenu)
public static void fillFontMenu(JMenu fontMenu)
//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; } }