Focus Cycle Sample : Focus « Swing JFC « Java

Focus Cycle Sample

Focus Cycle Sample
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

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

public class FocusCycleSample {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Focus Cycle Sample");

    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(3,3));
    for (int i = 0; i < 8; i++) {
      JButton button = new JButton("" + i);

    JPanel panel = new FocusCycleConstrainedJPanel();
    panel.setLayout(new GridLayout(1, 3));
    for (int i = 0; i < 3; i++) {
      JButton button = new JButton("" + (i + 3));

    frame.setSize(300, 200);

class FocusCycleConstrainedJPanel extends JPanel {
  public boolean isFocusCycleRoot() {
    return true;


Related examples in the same category

1.Button FocusButton Focus
2.Focus Traversal ExampleFocus Traversal Example
3.Focus ExampleFocus Example
4.Focus Concepts DemoFocus Concepts Demo
5.Track Focus Demo
6.Focus Traversal DemoFocus Traversal Demo
7.Use this method to determine whether a particular component has the focus
8.Handling Focus Changes
9.Requests focus for a component
10.Requests focus unless the component already has focus