accessibility

Setting an accessible name and mnemonic for an Image Button

With this tutorial we shall show you how to set an accessible name and a mnemonic for an ImageButton. Using mnemonics is highly recommended, because it enables users to choose and focus on specific items in the GUI environment using only keyboard shortcuts.

In short, to set an accessible name and mnemonic for an ImageButton one should follow these steps:

  • Create a new JFrame.
  • Use new JButton(new ImageIcon("image.png")) to create a new Image button.
  • Use getAccessibleContext().setAccessibleName("Button Name") to set the accessible name of the button.
  • Use setMnemonic to set a mnemonic for the button.

Let’s see the code:

package com.javacodegeeks.snippets.desktop;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextField;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;

public class ImageButtonMnemonic {

    public static void main(String[] args) {

// Create frame with specific title

Frame frame = new Frame("Example Frame");

/*

 * Create a container with a flow layout, which arranges its children

 * horizontally and center aligned. A container can also be created with

 * a specific layout using Panel(LayoutManager) constructor, e.g.

 * Panel(new FlowLayout(FlowLayout.RIGHT)) for right alignment

 */

Panel panel = new Panel();

// Create a component to add to the panel; in this case a text field with sample text

Component nameField = new TextField("Enter your name");

// Create a component to add to the panel; in this case a label for the name text field

JLabel nameLabel = new JLabel("Name:");

// Set a mnemonic on the label. The associated component will get the focus when the mnemonic is activated

nameLabel.setDisplayedMnemonic('N');

// make the association explicit

nameLabel.setLabelFor(nameField);

// Add label and field to the container

panel.add(nameLabel);

panel.add(nameField);

// Create a component to add to the frame; in this case an image button - change to where your image file is located

JButton button = new JButton(new ImageIcon("image.png"));

// The tool tip text, if set, serves as the accessible name for the button

button.setToolTipText("Button Name");

// If tool tip is being used for something else, set the accessible name.

button.getAccessibleContext().setAccessibleName("Button Name");

// Set mnemonic for the button

button.setMnemonic('B');

// Add the components to the frame; by default, the frame has a border layout

frame.add(panel, BorderLayout.NORTH);

frame.add(button, BorderLayout.SOUTH);

// Display the frame

int frameWidth = 300;

int frameHeight = 300;

frame.setSize(frameWidth, frameHeight);

frame.setVisible(true);

    }

}

 
This was an example on how to set an accessible name and mnemonic for an Image Button.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button