JFrame: setFocusTraversalPolicy(FocusTraversalPolicy lo) : JFrame « javax.swing « Java by API






JFrame: setFocusTraversalPolicy(FocusTraversalPolicy lo)

  
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 MainClass {

  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 = 9; i > 0; i--) {
      JButton button = new JButton(Integer.toString(i));
      frame.add(button, 0);
    }

    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.JFrame.DISPOSE_ON_CLOSE
2.JFrame.DO_NOTHING_ON_CLOSE
3.JFrame.EXIT_ON_CLOSE
4.JFrame: addComponentListener(ComponentListener l)
5.JFrame: addContainerListener(ContainerListener l)1
6.JFrame: addMouseWheelListener(MouseWheelListener l)
7.JFrame: addWindowListener(WindowListener lis)
8.JFrame: getAccessibleContext()
9.JFrame: getContentPane().getActionMap() [Bind key action to JFrame]
10.JFrame: getDefaultCloseOperation()
11.JFrame: getFontMetrics(Font font)
12.JFrame: getGlassPane()
13.JFrame: getLayeredPane()
14.JFrame: isFocusOwner()
15.JFrame: isUndecorated()
16.JFrame: processWindowEvent(WindowEvent e)
17.JFrame: setAlwaysOnTop(boolean alwaysOnTop)
18.JFrame: setBackground(Color c)
19.JFrame: setCursor(Cursor c)
20.JFrame: setDefaultButton(JButton defaultButton)
21.JFrame: setDefaultButton(JButton bn) (Remove 'Enter' key stroke from JTextField)
22.JFrame: setDefaultCloseOperation(int option)
23.JFrame: setDefaultLookAndFeelDecorated(boolean b)
24.JFrame: setExtendedState(int state)
25.JFrame: setFocusable(boolean focusable)
26.JFrame: setFocusableWindowState(boolean focusableWindowState)
27.JFrame: setIconImage(Image image)
28.JFrame: setJMenuBar(JMenuBar bar)
29.JFrame: setLocation(int x, int y)
30.JFrame: setLocationByPlatform(boolean locationByPlatform)
31.JFrame: setLocationRelativeTo(Component c)
32.JFrame: setMaximizedBounds(Rectangle bounds)
33.JFrame: setSize(int width, int height)
34.JFrame: setTitle(String title)
35.JFrame: setUndecorated(boolean b)
36.JFrame: setVisible(boolean visible)