Java JButton Settings addButtonInPanel(Container component, String text, ActionListener listener)

Here you can find the source of addButtonInPanel(Container component, String text, ActionListener listener)

Description

Add a new button to a JPanel and then add the panel 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

Return

Created button

Declaration

public static JButton addButtonInPanel(Container component, String text, ActionListener listener) 

Method Source Code


//package com.java2s;
/*/*from  w  w w  . ja v  a 2  s  .co m*/
 * Copyright 2007-2013 VTT Biotechnology
 * This file is part of Guineu.
 *
 * Guineu 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.
 *
 * Guineu 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
 * Guineu; 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.JButton;

import javax.swing.JPanel;

public class Main {
    /**
     * Add a new button to a JPanel and then add the panel 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 addButtonInPanel(Container component, String text, ActionListener listener) {
        return addButtonInPanel(component, text, listener, null);
    }

    /**
     * Add a new button to a JPanel and then add the panel 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 addButtonInPanel(Container component, String text, ActionListener listener,
            String actionCommand) {
        JPanel panel = new JPanel();
        JButton button = new JButton(text);
        if (listener != null) {
            button.addActionListener(listener);
        }
        if (actionCommand != null) {
            button.setActionCommand(actionCommand);
        }
        panel.add(button);
        if (component != null) {
            component.add(panel);
        }
        return button;
    }
}

Related

  1. addActionListeners(AbstractButton button, ActionListener[] listeners)
  2. addActionListenerToButtons(ActionListener l, AbstractButton... components)
  3. addButton(ActionListener l, Container container, String name, String cmd)
  4. addButton(Container component, String text, Icon icon, ActionListener listener, String actionCommand)
  5. addButton(String name, JPanel panel, ActionListener action)
  6. addButtonToPanel(JPanel panel, Object constraints, String text)
  7. addMiddleButtonDragSupport(Component targetComponent)
  8. addRadioButton(JPanel buttonPanel, ButtonGroup group, String buttonName, boolean selected, ActionListener listener)
  9. addToButtonGroup(ButtonGroup bg, T button)