Returns a button with the specified text. - Java Swing

Java examples for Swing:JButton

Description

Returns a button with the specified text.

Demo Code

/*//from ww  w.  j  a  va2 s . c om
 * 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.event.ActionListener;

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

import javax.swing.JButton;

public class Main {
    /**
     * Returns a button with the specified text.  If another property is
     * defined in the resource bundle with key
     * <code>key + ".Mnemonic"</code>, then it will be used for the mnemonic
     * of the button.
     *
     * @param bundle The resource bundle for localizing the button.
     * @param key The key into the bundle containing the string text value.
     * @return The button.
     * @see #newButton(ResourceBundle, String, String)
     * @see #newButton(ResourceBundle, String, ActionListener)
     * @see #newButton(ResourceBundle, String, String, ActionListener)
     */
    public static final JButton newButton(ResourceBundle bundle, String key) {
        return newButton(bundle, key, mnemonicKey(key));
    }

    /**
     * Returns a button with the specified text.  If another property is
     * defined in the resource bundle with key
     * <code>key + ".Mnemonic"</code>, then it will be used for the mnemonic
     * of the button.
     *
     * @param bundle The resource bundle for localizing the button.
     * @param key The key into the bundle containing the string text value.
     * @param listener If non-<code>null</code>, this listener will be
     *        added to the button.
     * @return The button.
     * @see #newButton(ResourceBundle, String, String)
     * @see #newButton(ResourceBundle, String, String, ActionListener)
     */
    public static final JButton newButton(ResourceBundle bundle,
            String key, ActionListener listener) {
        return newButton(bundle, key, mnemonicKey(key), listener);
    }

    /**
     * Returns a button with the specified text and mnemonic.
     *
     * @param bundle The resource bundle for localizing the button.
     * @param textKey The key into the bundle containing the string text value.
     * @param mnemonicKey The key into the bundle containing a single-char
     *        <code>String</code> value for the mnemonic.
     * @return The button.
     * @see #newButton(ResourceBundle, String)
     * @see #newButton(ResourceBundle, String, ActionListener)
     * @see #newButton(ResourceBundle, String, String, ActionListener)
     */
    public static final JButton newButton(ResourceBundle bundle,
            String textKey, String mnemonicKey) {
        return newButton(bundle, textKey, mnemonicKey, null);
    }

    /**
     * Returns a button with the specified text and mnemonic.
     *
     * @param bundle The resource bundle for localizing the button.
     * @param textKey The key into the bundle containing the string text value.
     * @param mnemonicKey The key into the bundle containing a single-char
     *        <code>String</code> value for the mnemonic.
     * @param listener If non-<code>null</code>, this listener will be
     *        added to the button.
     * @return The button.
     * @see #newButton(ResourceBundle, String)
     * @see #newButton(ResourceBundle, String, ActionListener)
     */
    public static final JButton newButton(ResourceBundle bundle,
            String textKey, String mnemonicKey, ActionListener listener) {
        JButton b = new JButton(bundle.getString(textKey));
        b.setMnemonic(getMnemonic(bundle, mnemonicKey));
        if (listener != null) {
            b.addActionListener(listener);
        }
        return b;
    }

    /**
     * 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";
    }

    /**
     * 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;
    }
}

Related Tutorials