Java JLabel Create createJLabel(Container c, String caption, int x, int y)

Here you can find the source of createJLabel(Container c, String caption, int x, int y)

Description

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

License

Open Source License

Parameter

Parameter Description
c the container
caption the caption (text) of the label
x x of its upper left corner
y y of its upper left corner

Return

a JLabel instance

Declaration

public static JLabel createJLabel(Container c, String caption, int x, int y) 

Method Source Code

//package com.java2s;
/*//from   ww w .j a  v  a  2  s. c  om
 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.FontMetrics;

import javax.swing.JLabel;

public class Main {
    /**
     * This method makes it easy (and also neat) creating a <code>JLabel</code>
     * and adding it to its container class.
     * <p>
     * The <code>BorderLayout</code> of the container class must be set to
     * <code>null</code>.
     * <p>
     * This method makes the label autosized. This is done according to the
     * container's font.
     * 
     * @param c
     *            the container
     * @param caption
     *            the caption (text) of the label
     * @param x
     *            x of its upper left corner
     * @param y
     *            y of its upper left corner
     * @return a <code>JLabel</code> instance
     */
    public static JLabel createJLabel(Container c, String caption, int x, int y) {
        FontMetrics fm = c.getFontMetrics(c.getFont());
        JLabel lbl = new JLabel(caption);
        lbl.setBounds(x, y, fm.stringWidth(caption) + 20, 20);
        c.add(lbl);
        return lbl;
    }
}

Related

  1. createCenteredLabel(JLabel label)
  2. createFormPanel(JLabel label, JComponent component)
  3. createJLabel(final String text, final Font font, final Color color)
  4. createJLabel(String mlabel)
  5. createJLabel(String text, Font font)
  6. createJLabel(String text, Font font)