Add Mnemonics to Inaccessible Buttons Within a Component

A standard in our shop is to provide keyboard shortcutswherever possible. We make extensive use of JOptionPaneobjects in dialogs for quick clarification and/or messages,but the buttons for “Yes”, “No”, “Ok”, and “Cancel” have noshortcuts. So here’s how we added the mnemonics. All childcomponents of a dialog (or any other component) are collectedinto an array, and are interrogated for the existence of buttons.If any are found, the text is compared against an array of valuesto identify those buttons that have the text we are looking for,and then the mnemonics can be added easily.

These static classes can be added to a utility class, andthe process can be easily adapted to add mnemonics forsub components of any type for any application. Also, it iscertainly a relief for the power keyboard user.

The following static method was adapted from the JOptionPane source code provided by Sun. However, this code has a few extra lines in the middle to call a method to add the mnemonics.

     public static int showConfirmDialog(        String msg, String title, int type, int option)    {        // Create option pane and dialog.        JOptionPane pane = new JOptionPane(msg, type, option);        JDialog dialog = pane.createDialog(null, title);        // Add mnemonics to buttons.        String[] text = { "Yes", "No", "Ok", "Cancel" };        int[] mnemonic = { 'Y', 'N', 'O', 'C' };        addMnemonicsToButtons(dialog, text, mnemonic);        dialog.show();        int result = JOptionPane.CLOSED_OPTION;        Object selectedValue = pane.getValue();        if (selectedValue != null)        {            if(selectedValue instanceof Integer)            {                result = ((Integer)selectedValue).intValue();            }        }        return result;    }

Here is the code that does the work, looping throughchild components, and checking to see if they are the buttons to which we would like to add the mnemonics.

     public static void addMnemonicsToButtons(        Component parentComponent, String[] text, int[] mnemonic)    {        Component[] component = getAllSubComponents(parentComponent);        int count = component.length;        for (int i = 0; i < count; i++)        {            if (component[i] instanceof AbstractButton)            {                AbstractButton b = (AbstractButton) component[i];                String btnText = b.getText();                if (btnText == null)                {                    btnText = "";                }                int textCount = text.length;                for (int j = 0; j < textCount; j++)                {                    if (btnText.equals(text[j]))                    {                        b.setMnemonic(mnemonic[j]);                    }                }            }        }    }

A Utility to get all sub components of a container.

     public static Component[] getAllSubComponents(Component c)    {        Vector v = new Vector();        addSubComponentsToVector(c, v);        int count = v.size();        Component[] children = new Component[count];        for (int i = 0; i < count; i++)        {            children[i] = (Component) v.elementAt(i);        }        v.removeAllElements();        return children;    }

Collect all child components of a given component into a givenvector.

     private static void addSubComponentsToVector(Component c, Vector v)    {        if (c == null)        {            return;        }        if (c instanceof Container)        {            Component[] children = ((Container) c).getComponents();            for (int i = 0; i < children.length; i++)            {                v.addElement(children[i]);                if (children[i] instanceof Container)                {                    addSubComponentsToVector(children[i], v);                }            }        }    }
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: