Container: setFocusTraversalPolicy(FocusTraversalPolicy policy) : Container « java.awt « Java by API






Container: setFocusTraversalPolicy(FocusTraversalPolicy policy)

  
import java.awt.Component;
import java.awt.Container;
import java.awt.FocusTraversalPolicy;
import java.awt.GridLayout;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SortingFocusTraversalPolicy;

public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Reverse Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(3, 3));
    for (int i = 0; i < 9; i++) {
      JButton button = new JButton(Integer.toString(i));
      frame.add(button);
    }
    final Container contentPane = frame.getContentPane();
    Comparator<Component> comp = new Comparator<Component>() {
      public int compare(Component c1, Component c2) {
        Component comps[] = contentPane.getComponents();
        List list = Arrays.asList(comps);
        int first = list.indexOf(c1);
        int second = list.indexOf(c2);
        return second - first;
      }
    };
    FocusTraversalPolicy policy = new SortingFocusTraversalPolicy(comp);
    frame.setFocusTraversalPolicy(policy);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}

   
    
  








Related examples in the same category

1.Container: addPropertyChangeListener(PropertyChangeListener listener)
2.Container: getComponentCount()
3.Container: getFocusTraversalPolicy()