Java JButton Settings addButton(Container component, String text, Icon icon, ActionListener listener, String actionCommand)

Here you can find the source of addButton(Container component, String text, Icon icon, ActionListener listener, String actionCommand)

Description

Add a new button to a given component

License

Open Source License

Parameter

Parameter Description
component Component to add the button to
text Button's text or null
icon Button's icon or null
listener Button's ActionListener or null
actionCommand Button's action command or null

Return

Created button

Declaration

public static JButton addButton(Container component, String text, Icon icon, ActionListener listener,
        String actionCommand) 

Method Source Code


//package com.java2s;
/*/*from  w  w  w  . j  a v a 2 s . com*/
 * Copyright 2006-2015 The MZmine 2 Development Team
 * 
 * This file is part of MZmine 2.
 * 
 * MZmine 2 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 2 of the License, or (at your option) any later
 * version.
 * 
 * MZmine 2 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
 * MZmine 2; if not, write to the Free Software Foundation, Inc., 51 Franklin
 * St, Fifth Floor, Boston, MA 02110-1301 USA
 */

import java.awt.Container;

import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.JButton;

public class Main {
    /**
     * Add a new button to a given component
     * 
     * @param component
     *            Component to add the button to
     * @param text
     *            Button's text or null
     * @param icon
     *            Button's icon or null
     * @param listener
     *            Button's ActionListener or null
     * @return Created button
     */
    public static JButton addButton(Container component, String text, Icon icon, ActionListener listener) {
        return addButton(component, text, icon, listener, null, 0, null);
    }

    /**
     * Add a new button to a given component
     * 
     * @param component
     *            Component to add the button to
     * @param text
     *            Button's text or null
     * @param icon
     *            Button's icon or null
     * @param listener
     *            Button's ActionListener or null
     * @param actionCommand
     *            Button's action command or null
     * @return Created button
     */
    public static JButton addButton(Container component, String text, Icon icon, ActionListener listener,
            String actionCommand) {
        return addButton(component, text, icon, listener, actionCommand, 0, null);
    }

    /**
     * Add a new button to a given component
     * 
     * @param component
     *            Component to add the button to
     * @param text
     *            Button's text or null
     * @param icon
     *            Button's icon or null
     * @param listener
     *            Button's ActionListener or null
     * @param actionCommand
     *            Button's action command or null
     * @param toolTip
     *            Button's tooltip text or null
     * @return Created button
     */
    public static JButton addButton(Container component, String text, Icon icon, ActionListener listener,
            String actionCommand, String toolTip) {
        return addButton(component, text, icon, listener, actionCommand, 0, toolTip);
    }

    /**
     * Add a new button to a given component
     * 
     * @param component
     *            Component to add the button to
     * @param text
     *            Button's text or null
     * @param icon
     *            Button's icon or null
     * @param listener
     *            Button's ActionListener or null
     * @param actionCommand
     *            Button's action command or null
     * @param mnemonic
     *            Button's mnemonic (virtual key code) or 0
     * @param toolTip
     *            Button's tooltip text or null
     * @return Created button
     */
    public static JButton addButton(Container component, String text, Icon icon, ActionListener listener,
            String actionCommand, int mnemonic, String toolTip) {
        JButton button = new JButton(text, icon);
        if (listener != null)
            button.addActionListener(listener);
        if (actionCommand != null)
            button.setActionCommand(actionCommand);
        if (mnemonic > 0)
            button.setMnemonic(mnemonic);
        if (toolTip != null)
            button.setToolTipText(toolTip);
        if (component != null)
            component.add(button);
        return button;
    }
}

Related

  1. addAbstractButtonListeners(AbstractButton b, Object... objs)
  2. addActionListener(ActionListener listener, AbstractButton... buttons)
  3. addActionListeners(AbstractButton button, ActionListener[] listeners)
  4. addActionListenerToButtons(ActionListener l, AbstractButton... components)
  5. addButton(ActionListener l, Container container, String name, String cmd)
  6. addButton(String name, JPanel panel, ActionListener action)
  7. addButtonInPanel(Container component, String text, ActionListener listener)
  8. addButtonToPanel(JPanel panel, Object constraints, String text)
  9. addMiddleButtonDragSupport(Component targetComponent)