Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Jan 28, 2000

Add Mnemonics to Inaccessible Buttons Within a Component

A standard in our shop is to provide keyboard shortcuts wherever possible. We make extensive use of JOptionPane objects in dialogs for quick clarification and/or messages, but the buttons for "Yes", "No", "Ok", and "Cancel" have no shortcuts. So here's how we added the mnemonics. All child components of a dialog (or any other component) are collected into an array, and are interrogated for the existence of buttons. If any are found, the text is compared against an array of values to 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, and the process can be easily adapted to add mnemonics for sub components of any type for any application. Also, it is certainly 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 given vector.
 
    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);
                }
            }
        }
    }

Roy Lachapelle
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap