Java JToolBar getToolbarButton(JToolBar toolbar, Action action)

Here you can find the source of getToolbarButton(JToolBar toolbar, Action action)

Description

Find the JButton in the toolbar that is associated with the provided action

License

Open Source License

Declaration

public static AbstractButton getToolbarButton(JToolBar toolbar, Action action) 

Method Source Code

//package com.java2s;
/*******************************************************************************
* Copyright (c) 2012 Firestar Software, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:/* ww w. java  2s . co  m*/
*     Firestar Software, Inc. - initial API and implementation
*
* Author:
*     Sally Conway
*
*******************************************************************************/

import java.awt.Component;
import java.awt.Container;
import javax.swing.AbstractButton;
import javax.swing.Action;

import javax.swing.JToolBar;

public class Main {
    /** Find the JButton in the toolbar that is associated with the provided action */
    public static AbstractButton getToolbarButton(JToolBar toolbar, Action action) {
        return getMenuItem(toolbar, action);
    }

    /** Recursively search the contents of the container to find a MenuItem or Button
     * that uses this action.
     * @param container
     * @param action
     * @return
     */
    public static AbstractButton getMenuItem(Container container, Action action) {
        for (int i = 0; i < container.getComponentCount(); i++) {
            Component item = container.getComponent(i);
            if (item instanceof AbstractButton && ((AbstractButton) item).getAction().equals(action)) {
                return (AbstractButton) item;
            } else if (item instanceof Container) {
                return getMenuItem((Container) item, action);
            }
        }
        return null;
    }
}

Related

  1. createToolBar()
  2. createToolbar()
  3. fixButton(AbstractButton button, String toolTip)
  4. floatToolBar(JToolBar tb, Point p)
  5. formatToolBar(JToolBar toolbar, int style)
  6. getToolBarFill(int orientation)
  7. getToolbarOrientation(String location)
  8. hideChildButtonsWithTooltip(Container parent, String tooltip)
  9. initIconButton(AbstractButton button, String toolTip)