Swing Focus Adapter



Swing Focus Adapter

  1. FocusAdapter class is an abstract adapter class in Java Swing.
  2. FocusAdapter class is for receiving keyboard focus events.
  3. Methods specified in FocusAdapter class are empty.
  4. FocusAdapter class exists as convenience for creating listener objects

Class declaration

Below is the declaration for java.awt.event.FocusAdapter class –

public abstract class FocusAdapter
   extends Object
      implements FocusListener

Class constructors

FocusAdapter()

Class methods

No.MethodReturn TypeParameterInvoked when
1focusGained()voidFocusEventcomponent gains the keyboard focus
2focusLost()voidFocusEventcomponent loses the keyboard focus

Methods inherited

This class inherits methods from the following classes:

java.lang.Object

Swing Focus Adapter Example

AdapterExample.java

package com.c4learn;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

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

public class AdapterExample {
   private JFrame baseFrame;
   private JLabel headLabel;
   private JLabel msgLabel;
   private JPanel controlPanel;

   public AdapterExample() {
      initializeUI();
   }

   public static void main(String[] args) {
      AdapterExample adapterDemo = new AdapterExample();
      adapterDemo.showFocusApapter();
   }

   private void initializeUI() {
      baseFrame = new JFrame("Java Swing Examples");
      baseFrame.setSize(500, 500);
      baseFrame.setLayout(new GridLayout(3, 1));

      headLabel = new JLabel("", JLabel.CENTER);
      msgLabel = new JLabel("", JLabel.CENTER);

      msgLabel.setSize(300, 100);

      baseFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent) {
            System.exit(0);
         }
      });

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      baseFrame.add(headLabel);
      baseFrame.add(controlPanel);
      baseFrame.add(msgLabel);
      baseFrame.setVisible(true);
   }

   private void showFocusApapter() {
      headLabel.setText("FocusAdapter Example");

      JButton btn1 = new JButton("Button 1");
      JButton btn2 = new JButton("Button 2");
      
      btn1.addFocusListener(new FocusAdapter() {
         public void focusGained(FocusEvent e) {
            msgLabel.setText("Button 1 - Got focus.");
         }
      });

      btn1.addFocusListener(new FocusAdapter() {
         public void focusLost(FocusEvent e) {
            msgLabel.setText("Button 1 - Lost focus.");
         }
      });

      controlPanel.add(btn1);
      controlPanel.add(btn2);
      baseFrame.setVisible(true);
   }
}

After running program in the eclipse you will see below output

focus adapter

Explanation

In this example consider below two buttons –

JButton btn1 = new JButton("Button 1");
JButton btn2 = new JButton("Button 2");

Now we have added focus listener for the button 1 –

btn1.addFocusListener(new FocusAdapter() {

   public void focusGained(FocusEvent e) {
	msgLabel.setText("Button 1 - Got focus.");
   }

   public void focusLost(FocusEvent e) {
	msgLabel.setText("Button 1 - Lost focus.");
   }

});

Now focusLost() method will provide the code to be executed when button1 will lost its focus. Similarly focusGained() method would be executed when button 1 gains focus.