Java Code Examples for javax.swing.JPopupMenu#getComponents()

The following examples show how to use javax.swing.JPopupMenu#getComponents() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PopupMenuHandler.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
private void rearrangeMenuItems(JPopupMenu popupMenu) {
    Component[] components = popupMenu.getComponents();
    Arrays.sort(components, (o1, o2) -> getGroupName(o1).compareToIgnoreCase(getGroupName(o2)));
    popupMenu.removeAll();
    Component lastComponent = null;
    String lastGroupName = null;
    for (Component component : components) {
        String groupName = getGroupName(component);
        if (lastGroupName != null
                && !lastGroupName.equals(groupName)
                && !(lastComponent instanceof JSeparator)) {
            popupMenu.addSeparator();
        }
        lastGroupName = groupName;
        lastComponent = component;
        if (component instanceof JMenuItem) {
            popupMenu.add((JMenuItem) component);
        } else if (component instanceof Action) {
            popupMenu.add((Action) component);
        } else {
            popupMenu.add(component);
        }
    }
}
 
Example 2
Source File: UtilitiesActionsTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testDynamicMenuContent() {
    Action[] acts = new Action[] {
        new Act1(),
        new Act2()
        
    };
    JPopupMenu popup = Utilities.actionsToPopup(acts, Lookup.EMPTY);
    Component[] comp = popup.getComponents();
    boolean onepresent = false;
    boolean twopresent = false;
    boolean threepresent = false;
    boolean fourpresent = false;
    boolean zeropresent = false;
    for (int i = 0; i < comp.length; i++) {
        if ("0".equals(((JMenuItem)comp[i]).getText())) {
            zeropresent = true;
        }
        if ("2".equals(((JMenuItem)comp[i]).getText())) {
            twopresent = true;
        }
        if ("4".equals(((JMenuItem)comp[i]).getText())) {
            fourpresent = true;
        }
        if ("3".equals(((JMenuItem)comp[i]).getText())) {
            threepresent = true;
        }
        if ("1".equals(((JMenuItem)comp[i]).getText())) {
            onepresent = true;
        }
    }
    assertTrue(threepresent);
    assertTrue(fourpresent);
    assertTrue(onepresent);
    assertTrue(twopresent);
    assertTrue(zeropresent);
}
 
Example 3
Source File: Command.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
public static void adjustMenuTree(JPopupMenu menu) {
    for (Component component : menu.getComponents()) {
        if (component instanceof JMenu) {
            adjustMenuTree((JMenu) component);
        } else if (component instanceof JMenuItem) {
            JMenuItem item   = (JMenuItem) component;
            Action    action = item.getAction();
            if (action instanceof Command) {
                ((Command) action).adjust();
            }
        }
    }
}