Add Mnemonics to Inaccessible Buttons Within a Component

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);;        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:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes