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.

Set focus Accelerator in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer