Java AWT KeyboardFocusManager set focus forward transversal key for a component

Description

Java AWT KeyboardFocusManager set focus forward transversal key for a component


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  w  w  .  jav a  2 s  . c  o  m*/
      frame.add(component2);
      frame.add(component3);

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

      Set set = new HashSet(component1.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));

      set.add(KeyStroke.getKeyStroke("F2"));
      component1.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set);

      
   }

}



PreviousNext

Related