Java JPanel get/set focus transversal key

Description

Java JPanel get/set focus transversal key


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 Main {
   public static void main(String[] args) {
      JFrame aWindow = new JFrame("java2s.com");
      aWindow.setBounds(0, 0, 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);/*w ww  . jav  a  2  s. co m*/
      System.out.println(set);

      p.setFocusTraversalKeys(KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, set);

      aWindow.add(p);

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



PreviousNext

Related