configure Text And Mnemonic - Java Swing

Java examples for Swing:Key Event

Description

configure Text And Mnemonic

Demo Code

/*//from  w w  w  .j ava2s .c  om
 * Copyright (C) 2006 Sun Microsystems, Inc. All rights reserved. Use is
 * subject to license terms.
 */
//package com.java2s;
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 Tutorials