Java JButton Settings configureTextAndMnemonic(AbstractButton button, String text)

Here you can find the source of configureTextAndMnemonic(AbstractButton button, String text)

Description

configure Text And Mnemonic

License

Open Source License

Declaration

public static void configureTextAndMnemonic(AbstractButton button, String text) 

Method Source Code


//package com.java2s;
/*/*w  ww. j a v  a2 s . co m*/
 * Copyright (C) 2006 Sun Microsystems, Inc. All rights reserved. Use is
 * subject to license terms.
 */

import java.awt.event.KeyEvent;
import javax.swing.AbstractButton;

public class Main {
    public static void configureTextAndMnemonic(AbstractButton button, String text) {
        int length = text.length();
        int index = text.indexOf('&');
        if (index == -1) {
            button.setText(text);
        } else {
            StringBuilder newText = new StringBuilder(length);
            int mnemonic = -1;
            int mnemonicIndex = -1;
            for (int i = 0; i < length; i++) {
                char aChar = text.charAt(i);
                if (aChar == '\\') {
                    if (i + 1 < length && text.charAt(i + 1) == '&') {
                        i++;
                        newText.append('&');
                    } else {
                        newText.append(aChar);
                    }
                } else if (aChar == '&') {
                    if (i + 1 < length) {
                        if (mnemonic != -1) {
                            throw new IllegalArgumentException("Mnemonic already defined " + text);
                        }
                        aChar = text.charAt(i + 1);
                        if (aChar >= 'a' && aChar <= 'z') {
                            mnemonic = KeyEvent.VK_A + (aChar - 'a');
                        } else if (aChar >= 'A' && aChar <= 'Z') {
                            mnemonic = KeyEvent.VK_A + (aChar - 'A');
                        } else {
                            throw new IllegalArgumentException("Not valid mnemonic " + text);
                        }
                        mnemonicIndex = newText.length();
                    } else {
                        newText.append(aChar);
                    }
                } else {
                    newText.append(aChar);
                }
            }
            button.setText(newText.toString());
            if (mnemonic != -1) {
                button.setMnemonic(mnemonic);
                button.setDisplayedMnemonicIndex(mnemonicIndex);
            }
        }
    }
}

Related

  1. askYesNoQuestion(String dlgTitle, String question, Component parent, Object[] buttonTittle)
  2. buttonGroup(AbstractButton b1, AbstractButton b2)
  3. buttonGroup(JToggleButton b1, JToggleButton b2)
  4. clickButton(final Window window, final String buttonText)
  5. configureOKAndCancelButtons(JPanel panel, Action ok, Action cancel)
  6. confirm(Component comp, String title, String message, int buttons, int type)
  7. creaStyledButton(int style)
  8. customize(AbstractButton btn)
  9. decoratedToSimpleButton(final T button)