Home » Desktop Java » awt » event » FocusListener example

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

FocusListener example

This is an example that discusses how to use FocusListener in Java. This is a very handy feature when you have several components and you want to monitor and handle the event when on of them gains or looses focus.

In short, all you have to do in order to work with a FocusListener is:

  • Create a new FocusListener
  • Override the methods that correspond to the events that you want to monitor about the component e.g focusGained, focusLost and customize as you wish the handling of the respective events. Now every time the monitored component gains or looses focus the corresponding method will be executed.
  • Use the addFocusListener method of the component you want to monitor, in order to add the FocusListener you’ve created.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.desktop;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;

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

public class FocusListenerExample {

    public static void main(String args[]) {

  JFrame jFrame = new JFrame();

  Container cPan = jFrame.getContentPane();

  FocusListener focusListener = new FocusListener() {


public void focusGained(FocusEvent event) {




public void focusLost(FocusEvent event) {



private void printInfo(FocusEvent event) {

    System.out.println("Source  : " + getComponentName(event.getComponent()));

    System.out.println("Opposite : "

+ getComponentName(event.getOppositeComponent()));

    System.out.println("Temporary: " + event.isTemporary());


private String getComponentName(Component component) {

    return (component == null) ? null : component.getName();



  JPanel jPanel = new JPanel();

  JLabel jLabel = new JLabel("Label 1: ");

  JTextField textField = new JTextField("Type Here", 15);







  cPan.add(jPanel, BorderLayout.NORTH);

  jPanel = new JPanel();

  jLabel = new JLabel("Label 2: ");

  textField = new JTextField("14.0", 10);








  cPan.add(jPanel, BorderLayout.SOUTH);



This was an example on how to use FocusListener in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments