displayedMnemonic and labelFor properties

The following example shows how the displayedMnemonic and labelFor properties can be used to direct focus to a component based on the mnemonic assigned to a label. All we do here is create three labels and three text fields, assigning one field to each label:

import javax.swing.*;
import java.awt.*;

// Shows how displayedMnemonic and labelFor properties work together
public class MnemonicLabels {
	public static void main(String[] args) {

		JTextField firstField = new JTextField(10);
		JTextField middleField = new JTextField(10);
		JTextField lastField = new JTextField(10);

		// Create labels and mnemonics.
		JLabel firstLabel = new JLabel("First Name", JLabel.RIGHT);
		firstLabel.setDisplayedMnemonic('F');
		firstLabel.setLabelFor(firstField);

		JLabel middleLabel = new JLabel("Middle Initial", JLabel.RIGHT);
		middleLabel.setDisplayedMnemonic('I');
		middleLabel.setDisplayedMnemonicIndex(7); // Requires 1.4
		middleLabel.setLabelFor(middleField);

		JLabel lastLabel = new JLabel("Last Name", JLabel.RIGHT);
		lastLabel.setDisplayedMnemonic('L');
		lastLabel.setLabelFor(lastField);

		// Layout and display
		JPanel p = new JPanel();
		p.setLayout(new GridLayout(3, 2, 5, 5));
		p.add(firstLabel);
		p.add(firstField);
		p.add(middleLabel);
		p.add(middleField);
		p.add(lastLabel);
		p.add(lastField);

		JFrame f = new JFrame("MnemonicLabels");
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setContentPane(p);
		f.pack();
		f.setVisible(true);
	}
}

The first letter in each label is underlined, based on the assigned mnemonic. Pressing Alt-F, Alt-I, or Alt-L causes focus to shift to the corresponding text field.

Share

You may also like...

No © Loop and Break