Java JToolBar addToolBarButton(JToolBar toolbar, Action action, String tooltip, Icon icon, String lbl)

Here you can find the source of addToolBarButton(JToolBar toolbar, Action action, String tooltip, Icon icon, String lbl)

Description

Add an action to the toolbar.

License

Open Source License

Declaration

public static JButton addToolBarButton(JToolBar toolbar, Action action,
        String tooltip, Icon icon, String lbl) 

Method Source Code

//package com.java2s;
/*//from   ww w.  j  a v  a 2s  .c o m
 Copyright (c) 1998-2013 The Regents of the University of California
 All rights reserved.
 Permission is hereby granted, without written agreement and without
 license or royalty fees, to use, copy, modify, and distribute this
 software and its documentation for any purpose, provided that the above
 copyright notice and the following two paragraphs appear in all copies
 of this software.

 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.

 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 PROVIDED HEREUNDER IS ON AN  BASIS, AND THE UNIVERSITY OF
 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
 ENHANCEMENTS, OR MODIFICATIONS.

 PT_COPYRIGHT_VERSION_2
 COPYRIGHTENDKEY
 */

import java.awt.Insets;

import javax.swing.Action;
import javax.swing.Icon;

import javax.swing.JButton;

import javax.swing.JToolBar;

public class Main {
    /** This key is used in an action to specify an icon used in toolbars.
     */
    public static final String LARGE_ICON = "LargeIcon";
    /** This key is used in an action to specify a rollover icon
     *  used in toolbars.
     */
    public static final String ROLLOVER_ICON = "rolloverIcon";
    /** This key is used in an action to specify a rollover selected icon
     *  used in toolbars.
     */
    public static final String ROLLOVER_SELECTED_ICON = "rolloverSelectedIcon";
    /** This key is used in an action to specify a selected icon
     *  used in toolbars.
     */
    public static final String SELECTED_ICON = "selectedIcon";

    /** Add the action to the given toolbar.
     *  If the LARGE_ICON property is specified in the Action, then use it
     *  for the button.  We use this instead of SMALL_ICON, because
     *  SMALL_ICON shows up when the action is added to a menu, and in
     *  most cases we don't actually want an icon there.
     *  If no icon is specified, then the button will just
     *  have the name of the action.  If the "tooltip" property is specified
     *  in the action, then create a tooltip for the button with the string.
     *  The new button is added to the action as the "toolButton" property.
     *  The button is enabled by default.
     */
    public static JButton addToolBarButton(JToolBar toolbar, Action action) {
        Icon icon = (Icon) action.getValue(LARGE_ICON);
        String label = null;

        if (icon == null) {
            label = (String) action.getValue(Action.NAME);
        }

        return addToolBarButton(toolbar, action, null, icon, label, true);
    }

    /** Add an action to the toolbar.  If the tool tip is null, use
     * the "tooltip" property already in the action, otherwise add the
     * property to the action. The new button is added to the action
     * as the "toolButton" property.  The button represented by an icon
     * (no text) and is enabled by default.
     */
    public static JButton addToolBarButton(JToolBar toolbar, Action action,
            String tooltip, Icon icon) {
        return addToolBarButton(toolbar, action, tooltip, icon, null, true);
    }

    /** Add an action to the toolbar.  If the tool tip is null, use
     * the "tooltip" property already in the action, otherwise add the
     * property to the action. The new button is added to the action
     * as the "toolButton" property.  The button represented by an icon
     * (no text) and is enabled by default.
     */
    public static JButton addToolBarButton(JToolBar toolbar, Action action,
            String tooltip, Icon icon, boolean isEnabled) {
        return addToolBarButton(toolbar, action, tooltip, icon, null,
                isEnabled);
    }

    /** Add an action to the toolbar.  If the tool tip is null, use
     * the "tooltip" property already in the action, otherwise add the
     * property to the action. The new button is added to the action
     * as the "toolButton" property.  The button represented by text
     * (no icon) and is enabled by default.
     */
    public static JButton addToolBarButton(JToolBar toolbar, Action action,
            String tooltip, String lbl) {
        return addToolBarButton(toolbar, action, tooltip, null, lbl, true);
    }

    /** Add an action to the toolbar.  If either an icon or a text string
     * are specified (non-null), they are added.  The button is enabled by
     * default.
     */
    public static JButton addToolBarButton(JToolBar toolbar, Action action,
            String tooltip, Icon icon, String lbl) {
        return addToolBarButton(toolbar, action, tooltip, icon, lbl, true);
    }

    /** Add an action to the toolbar.  If the tool tip is null, use
     * the "tooltip" property already in the action, otherwise add the
     * property to the action.  The new button is added to the action
     * as the "toolButton" property.  If either an icon or a text string
     * are specified (non-null), they are added.
     */
    public static JButton addToolBarButton(JToolBar toolbar, Action action,
            String tooltip, Icon icon, String lbl, boolean isEnabled) {
        if (tooltip == null) {
            tooltip = (String) action.getValue("tooltip");
        } else {
            action.putValue("tooltip", tooltip);
        }

        JButton button = toolbar.add(action);
        button.setToolTipText(tooltip);
        button.setText(null);
        button.setRequestFocusEnabled(false);

        if (icon != null) {
            button.setIcon(icon);
        }

        if (lbl != null) {
            button.setText(lbl);
        }

        Icon rolloverIcon = (Icon) action.getValue(ROLLOVER_ICON);
        if (rolloverIcon != null) {
            button.setRolloverIcon(rolloverIcon);
        }
        Icon rolloverSelectedIcon = (Icon) action
                .getValue(ROLLOVER_SELECTED_ICON);
        if (rolloverSelectedIcon != null) {
            button.setRolloverSelectedIcon(rolloverSelectedIcon);
        }
        Icon selectedIcon = (Icon) action.getValue(SELECTED_ICON);
        if (selectedIcon != null) {
            button.setSelectedIcon(selectedIcon);
        }

        button.setMargin(new Insets(0, 0, 0, 0));

        //        button.setBorderPainted(false);
        button.setBorderPainted(true);
        button.setEnabled(isEnabled);
        action.putValue("toolBarButton", button);
        return button;
    }
}

Related

  1. addButton(Container component, String text, Icon icon, ActionListener listener, String actionCommand, int mnemonic, String toolTip)
  2. addButton(Container container, String label, int mnemonic, String tooltip, String placement, boolean debug)
  3. addRadioButton(Container container, String label, int mnemonic, String tooltip, String placement, boolean debug)
  4. addToolbar(JPanel panel, JToolBar bar)
  5. addToolBarButton(JToolBar toolbar, Action action, String tooltip, Icon icon)
  6. addToolItems(JToolBar toolBar, JComponent... items)
  7. buildToolbar(final JToolBar toolbar, final List components)
  8. createDefaultToolBar()
  9. createToolBar()