KeyboardFocusManager : Focus « Swing Event « Java Tutorial






Tab and Shift-Tab are used for keyboard focus traversal.

To define your own traversal keys: replace or append to a key set via the setFocusTraversalKeys() method of Component.

FORWARD_ TRAVERSAL_KEYS, BACKWARD_TRAVERSAL_KEYS, and UP_CYCLE_TRAVERSAL_KEYS constants of KeyboardFocusManager are for forward, backward, and up-cycle.

To add the 'A' key as an up-cycle key for a component

KeyboardFocusManager
import java.awt.AWTKeyStroke;
import java.awt.KeyboardFocusManager;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

public class KeyboardFocusManagerDemo {
  public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Border Layout");
    aWindow.setBounds(30, 30, 300, 300); // Size
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel p = new JPanel();

    p.add(new JTextField(10));
    p.add(new JTextField(10));
    p.add(new JTextField(10));
    p.add(new JTextField(10));
    p.add(new JTextField(10));
    p.add(new JTextField(10));

    Set<AWTKeyStroke> set = p.getFocusTraversalKeys(KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS);
    set = new HashSet(set);
    KeyStroke up = KeyStroke.getKeyStroke("A");
    set.add(up);
    System.out.println(set);

    p.setFocusTraversalKeys(KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, set);

    aWindow.add(p);

    aWindow.setVisible(true); // Display the window
  }
}








15.15.Focus
15.15.1.No Focus Button
15.15.2.Focus auto-settingFocus auto-setting
15.15.3.Focus Traversal: requestFocusInWindowFocus Traversal: requestFocusInWindow
15.15.4.Moving focus to the next component: focusNextComponent()Moving focus to the next component: focusNextComponent()
15.15.5.Restricting the Focus CycleRestricting the Focus Cycle
15.15.6.Request Focus inside a windowRequest Focus inside a window
15.15.7.Predefined Focus Traversal Policies
15.15.8.Reversing Focus TraversalReversing Focus Traversal
15.15.9.KeyboardFocusManagerKeyboardFocusManager
15.15.10.Use KeyboardFocusManagerUse KeyboardFocusManager
15.15.11.Use FocusTraversalPolicy
15.15.12.Finding the Next Focusable Component
15.15.13.Find the previous focusable component
15.15.14.Determining If a Focus Lost Is Temporary or Permanent
15.15.15.Null is returned if none of the components in this application has the focus
15.15.16.Null is returned if none of the windows in this application has the focus
15.15.17.Changes the focus traversal keys for the entire application.
15.15.18.Change the backward focus traversal keys for the application
15.15.19.Change the forward focus traversal keys for a component
15.15.20.JComponent.setFocusTraversalKeys(int arg0, Set arg1)
15.15.21.KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS
15.15.22.Setting Focus Traversal Keys for the Entire Application
15.15.23.KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS
15.15.24.KeyboardFocusManager.getCurrentKeyboardFocusManager().getDefaultFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS)
15.15.25.KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner()
15.15.26.Determining the Opposite Component of a Focus Event
15.15.27.Listening to All Key Events Before Delivery to Focused Component
15.15.28.Removing the Focus from the Application
15.15.29.Use isFocusOwner to determine whether a particular component has the focus
15.15.30.JComponent.WHEN_IN_FOCUSED_WINDOW
15.15.31.Make sure that my Text field has the focus when a JFrame is created