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

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

Description

Add an action to the toolbar.

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;

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.
     *///w w w .  ja  v a 2 s  .c  om
    public static final String LARGE_ICON = "LargeIcon";

    /** 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);
        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, String lbl)
  6. addToolItems(JToolBar toolBar, JComponent... items)
  7. buildToolbar(final JToolBar toolbar, final List components)
  8. createDefaultToolBar()