Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import javax.swing.*;

import java.util.ArrayList;

public class Main {
    /**
     * Make a popu menu using lists of actions. Each action list will be separated by
     * a menu separator. Empty action lists, or null-valued action lists will be ignored as if
     * the were not there.
     * @param aActionLists
     * @return A popup menu.
     */
    public static JPopupMenu popupBuilder(Action[]... aActionLists) {
        // Prepare the popup.
        final JPopupMenu lMenu = new JPopupMenu();

        // First we are going to filter out the empty lists.
        final java.util.List<Action[]> lActionList = new ArrayList<Action[]>();
        for (Action[] lList : aActionLists)
            if (lList != null && lList.length > 0)
                lActionList.add(lList);

        // Build the popup menu.
        for (int i = 0; i < lActionList.size(); i++) {
            for (Action lAction : lActionList.get(i))
                lMenu.add(lAction);
            if (i < lActionList.size() - 1)
                lMenu.addSeparator();
        }
        return lMenu;
    }
}