Set focus Accelerator in Java
Description
The following code shows how to set focus Accelerator.
Example
/* w w w. java2 s. com*/
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Main {
public static void main(String[] args) {
JFrame f = new JFrame("Text Accelerator Example");
Container cp = f.getContentPane();
cp.setLayout(new GridLayout(2,2));
JLabel l = new JLabel("Name:");
cp.add(l);
l.setDisplayedMnemonic('n');
l = new JLabel("House/Street:");
cp.add(l);
l.setDisplayedMnemonic('h');
JTextField t = new JTextField(35);
cp.add(t);
t.setFocusAccelerator('n');
t = new JTextField(35);
cp.add(t);
t.setFocusAccelerator('h');
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »