Java JPanel set focus traversal policy provider

Description

Java JPanel set focus traversal policy provider

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {
   public static void main(String args[]) {
      JFrame frame = new JFrame("Focus Cycle Sample");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLayout(new GridLayout(3, 3));
      //w w  w.  ja  v  a  2  s.c  o m
      for (int i = 0; i < 3; i++) {
         JButton button = new JButton("" + i);
         frame.add(button);
      }
      
      JPanel panel = new JPanel();
      panel.setFocusCycleRoot(true);
      panel.setFocusTraversalPolicyProvider(true);
      
      panel.setLayout(new GridLayout(1, 3));
      for (int i = 0; i < 3; i++) {
         JButton button = new JButton("" + (i + 3));
         panel.add(button);
      }
      frame.add(panel);
      
      frame.add(new JButton("A"));
      frame.add(new JButton("B"));
      frame.add(new JButton("C"));
      
      frame.setSize(300, 200);
      frame.setVisible(true);
   }
}



PreviousNext

Related