Java Swing Menu addBooleanActionTo(Container menuOrToolBar, Action action)

Here you can find the source of addBooleanActionTo(Container menuOrToolBar, Action action)

Description

Creates a JCheckBoxMenuItem for a menu or JToggleButton for a tool bar

License

Open Source License

Parameter

Parameter Description
menuOrToolBar the menu or tool bar
action the action

Declaration

public static void addBooleanActionTo(Container menuOrToolBar, Action action) 

Method Source Code


//package com.java2s;
/*/*ww w  .ja va2s  .c om*/
 * Copyright 1997-2013 Fabien Michel, Olivier Gutknecht, Jacques Ferber
 * 
 * This file is part of MaDKit.
 * 
 * MaDKit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MaDKit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MaDKit. If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Component;
import java.awt.Container;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.swing.Action;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;

import javax.swing.JToggleButton;

public class Main {
    /**
     * Creates a {@link JCheckBoxMenuItem} for a menu or {@link JToggleButton} for a
     * tool bar
     * 
     * @param menuOrToolBar the menu or tool bar
     * @param action the action
     * 
     */
    public static void addBooleanActionTo(Container menuOrToolBar, Action action) {
        Method addButton;
        try {
            addButton = Container.class.getMethod("add", Component.class);
            if (menuOrToolBar instanceof JMenu) {
                addButton.invoke(menuOrToolBar, new JCheckBoxMenuItem(action));
            } else {
                final JToggleButton jToggleButton = new JToggleButton(action);
                jToggleButton.setText(null);
                addButton.invoke(menuOrToolBar, jToggleButton);
            }
        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
                | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

Related

  1. addHoverEffect4MenuAbout(final Component component, final Color overbgcolor, final Color overfgcolor, final Color outbgcolor, final Color outfgcolor)
  2. addRCMenuMouseListener(final JTextComponent text)
  3. appendMenuSubElements(MenuElement element, StringBuilder builder, String indent)
  4. applyContextMenuFontRecurse(MenuElement item, Font font)