Returns an JLabel with the specified text. - Java Swing

Java examples for Swing:JLabel

Description

Returns an JLabel with the specified text.

Demo Code

/*/* w  w  w.  ja v  a 2  s  .  c o m*/
 * 09/08/2005
 *
 * UIUtil.java - Utility methods for org.fife.ui classes.
 * Copyright (C) 2005 Robert Futrell
 * http://fifesoft.com/rtext
 * Licensed under a modified BSD license.
 * See the included license file for details.
 */
//package com.java2s;

import java.awt.Component;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.swing.JLabel;

public class Main {
    /**
     * Returns an <code>JLabel</code> with the specified text.  If another
     * property with name <code>key + ".Mnemonic"</code> is defined, it is
     * used as the mnemonic for the label.
     *
     * @param msg The resource bundle.
     * @param key The key into the bundle containing the string text value.
     * @return The <code>JLabel</code>.
     */
    public static final JLabel newLabel(ResourceBundle msg, String key) {
        return newLabel(msg, key, null);
    }

    /**
     * Returns an <code>JLabel</code> with the specified text.  If another
     * property with name <code>key + ".Mnemonic"</code> is defined, it is
     * used as the mnemonic for the label.
     *
     * @param msg The resource bundle.
     * @param key The key into the bundle containing the string text value.
     * @param labelFor If non-<code>null</code>, the label is a label for
     *        that specific component.
     * @return The <code>JLabel</code>.
     */
    public static final JLabel newLabel(ResourceBundle msg, String key,
            Component labelFor) {
        JLabel label = new JLabel(msg.getString(key));
        label.setDisplayedMnemonic(getMnemonic(msg, mnemonicKey(key)));
        if (labelFor != null) {
            label.setLabelFor(labelFor);
        }
        return label;
    }

    /**
     * Returns the mnemonic specified by the given key in a resource bundle.
     * 
     * @param msg The resource bundle.
     * @param key The key for the mnemonic.
     * @return The mnemonic, or <code>0</code> if not found.
     */
    public static final int getMnemonic(ResourceBundle msg, String key) {
        int mnemonic = 0;
        try {
            Object value = msg.getObject(key);
            if (value instanceof String) {
                mnemonic = ((String) value).charAt(0);
            }
        } catch (MissingResourceException mre) {
            // Swallow.  TODO: When we drop 1.4/1.5 support, use containsKey().
        }
        return mnemonic;
    }

    /**
     * Returns the default key for a button or menu item's mnemonic, based
     * on its root key.
     * 
     * @param key The key.
     * @return The mnemonic key.
     */
    private static final String mnemonicKey(String key) {
        return key + ".Mnemonic";
    }
}

Related Tutorials