Java JButton Create createJButton(Container c, String caption, int x, int y, int width, int height, ActionListener al)

Here you can find the source of createJButton(Container c, String caption, int x, int y, int width, int height, ActionListener al)

Description

This method makes it easy (and also neat) creating a JButton and adding it to its container class.

License

Open Source License

Parameter

Parameter Description
c the container
caption the caption (text) of the control
x x of its upper left corner
y y of its upper left corner
width width of the component
height height of the component
al the <code>ActionListener</code> of the button, set it <code>null</code> if you will do it manually later.

Return

a JButton instance

Declaration

public static JButton createJButton(Container c, String caption, int x, int y, int width, int height,
        ActionListener al) 

Method Source Code

//package com.java2s;
/*/*from www.  j  a va2s. co  m*/
 This file is part of Coach Assistant
 Copyright (C) 2004-2006 Sina Iravanian  <sina_iravanian@yahoo.com>
    
 This program 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; version 2 of the License.
    
 This program 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 this program; if not, write to the Free Software
 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

import java.awt.Container;

import java.awt.event.ActionListener;

import javax.swing.JButton;

public class Main {
    /**
     * This method makes it easy (and also neat) creating a <code>JButton</code>
     * and adding it to its container class.
     * <p>
     * The <code>BorderLayout</code> of the container class must be set to
     * <code>null</code>.
     * 
     * @param c
     *            the container
     * @param caption
     *            the caption (text) of the control
     * @param x
     *            x of its upper left corner
     * @param y
     *            y of its upper left corner
     * @param width
     *            width of the component
     * @param height
     *            height of the component
     * @param al
     *            the <code>ActionListener</code> of the button, set it
     *            <code>null</code> if you will do it manually later.
     * @return a <code>JButton</code> instance
     */
    public static JButton createJButton(Container c, String caption, int x, int y, int width, int height,
            ActionListener al) {
        JButton btn = new JButton(caption);
        btn.setBounds(x, y, width, height);
        if (al != null)
            btn.addActionListener(al);

        c.add(btn);
        return btn;
    }
}

Related

  1. CreateFlatButton()
  2. createGameRadioButton(String answer, int fontSize)
  3. createHyperlinkButton(String text, String tip, ActionListener action)
  4. createIconButton(Class klass, String resource, String fallbackText)
  5. createIconButton(ImageIcon icon, int dimension, String tooltipText, java.awt.event.ActionListener action)
  6. createJButton(final String label, final Font font)
  7. createJButton(String label, Font font)
  8. createJButton(String text, String name, ActionListener a)
  9. createLabelButton(String text)