Java AWT KeyboardFocusManager set focus forward transversal key

Description

Java AWT KeyboardFocusManager set focus forward transversal key


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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.KeyStroke;

public class Main {
   public static void main(String[] argv) {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      JButton component1 = new JButton("1");
      JButton component2 = new JButton("2");
      JButton component3 = new JButton("3");

      frame.setLayout(new FlowLayout());
      frame.add(component1);//from w  ww  .j  a  v  a2 s .  c  om
      frame.add(component2);
      frame.add(component3);

      frame.pack();
      frame.setVisible(true);

      Set<AWTKeyStroke> set = new HashSet<AWTKeyStroke>(KeyboardFocusManager.getCurrentKeyboardFocusManager()
            .getDefaultFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));

      set.add(KeyStroke.getKeyStroke("F2"));
      KeyboardFocusManager.getCurrentKeyboardFocusManager()
            .setDefaultFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set);
   }

}



PreviousNext

Related