Java AWT KeyboardFocusManager focus next component

Description

Java AWT KeyboardFocusManager focus next component

import java.awt.GridLayout;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

class ActionFocusMover implements ActionListener {
   public void actionPerformed(ActionEvent actionEvent) {
      KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
      manager.focusNextComponent();/*  w w w . j  a  v a2 s. c  o  m*/
   }
}

public class Main {
   public static void main(String args[]) {
      JFrame frame = new JFrame("Focus Sample");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      ActionFocusMover listener = new ActionFocusMover();
      frame.setLayout(new GridLayout(3, 3));
      JButton button = new JButton("0");
      button.addActionListener(listener);
      button.setFocusable(false);
      frame.add(button);

      button = new JButton("1");
      button.addActionListener(listener);
      frame.add(button);

      button = new JButton("2");
      button.addActionListener(listener);
      frame.add(button);
      
      frame.setSize(300, 200);
      frame.setVisible(true);
      frame.setSize(300, 200);
      frame.setVisible(true);
   }
}



PreviousNext

Related