Java examples for Swing:JButton
Returns a radio button with the specified properties.
/*/*ww w . j a v a 2 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.ButtonGroup; import javax.swing.JRadioButton; public class Main { /** * Returns a radio button with the specified properties. * * @param msg The resource bundle in which to get properties. * @param keyRoot The key into the bundle containing the radio button's * label. If another property is defined with the name * <code>keyRoot + ".Mnemonic"</code>, it is used for the * mnemonic for the radio button. * @param bg If non-<code>null</code>, the radio button is added to the * button group. * @return The <code>JRadioButton</code>. * @see #newRadio(ResourceBundle, String, ButtonGroup, ActionListener) * @see #newRadio(ResourceBundle, String, ButtonGroup, ActionListener, boolean) */ public static final JRadioButton newRadio(ResourceBundle msg, String keyRoot, ButtonGroup bg) { return newRadio(msg, keyRoot, bg, null, false); } /** * Returns a radio button with the specified properties. * * @param msg The resource bundle in which to get properties. * @param keyRoot The key into the bundle containing the radio button's * label. If another property is defined with the name * <code>keyRoot + ".Mnemonic"</code>, it is used for the * mnemonic for the radio button. * @param bg If non-<code>null</code>, the radio button is added to the * button group. * @param listener If non-<code>null</code>, the listener is added to * the radio button. * @return The <code>JRadioButton</code>. * @see #newRadio(ResourceBundle, String, ButtonGroup) * @see #newRadio(ResourceBundle, String, ButtonGroup, ActionListener, boolean) */ public static final JRadioButton newRadio(ResourceBundle msg, String keyRoot, ButtonGroup bg, ActionListener listener) { return newRadio(msg, keyRoot, bg, listener, false); } /** * Returns a radio button with the specified properties. * * @param msg The resource bundle in which to get properties. * @param keyRoot The key into the bundle containing the radio button's * label. If another property is defined with the name * <code>keyRoot + ".Mnemonic"</code>, it is used for the * mnemonic for the radio button. * @param bg If non-<code>null</code>, the radio button is added to the * button group. * @param listener If non-<code>null</code>, the listener is added to * the radio button. * @param selected Whether the radio button is initially selected. * @return The <code>JRadioButton</code>. * @see #newRadio(ResourceBundle, String, ButtonGroup) * @see #newRadio(ResourceBundle, String, ButtonGroup, ActionListener) */ public static final JRadioButton newRadio(ResourceBundle msg, String keyRoot, ButtonGroup bg, ActionListener listener, boolean selected) { JRadioButton radio = new JRadioButton(msg.getString(keyRoot)); radio.setMnemonic(getMnemonic(msg, mnemonicKey(keyRoot))); if (bg != null) { bg.add(radio); } if (listener != null) { radio.addActionListener(listener); } radio.setSelected(selected); return radio; } /** * 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"; } }