FocusListener in Java

Introduction

In this article, we discuss how to implement FocusListener in Java. The Netbeans IDE is used for the development of the example.

What is FocusListener

FocusListener is basically an interface that for receiving Keyboard focus events on components.

Methods Inside focusListener

  • void focusGained(FocusEvent e)

         This method is called when the component gains the keyboard focus.

  • void focusLost(FocusEvent e)

         This method is called when the component losses the keyboard focus.

Packages Imported

javax.swing.*;

This package is imported to provide a graphical user interface for programs, it is an extended version of AWT. The component of Swing are JFrmae, JLABEL, JScrollPane and so on.

java.awt.*;

AWT stands for Abstract Window Toolkit, this package is imported to use components of AWT, like label, list, combobox and so on.

java.awt.event.*;

This package is imported to handle the various action events.

Example

In this example; we implement a Focus Listener in Java using the Netbeans IDE. There are certain steps in the Netbeans IDE that we need to follow as explained below.

Step 1

Open the Netbeans IDE

1.jpg

Step 2

Click on "File" from the Menu bar as in the following:

2.jpg

Step 3

Click on "New Project" as in the following:

3.jpg

Step 4

Select "Java" and "Java application" then click on "Next" as in the following:

4.jpg

Step 5

Instead of project name specify "FocusListenerDemo" and instead of main class also specify "FocusListenerDemo" and click on "Finish".

5.jpg

Step 6

In this class write the following code (the class name is "FocusListenrDemo"):

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

public class FocusEventDemo implements FocusListener

{   

 JTextArea ta;

 public static void main(String[] args)

 {      

  EventQueue.invokeLater(new Runnable()

  {            

   @Override

   public void run()

   {                

    new FocusEventDemo().BuildGUI();        

   }

  });             

 }

 public void BuildGUI()

  {

   JFrame f = new JFrame();      

   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   f.setTitle("Creating a Table Example");

   f.setSize(700,200);       

   f.setLocationRelativeTo(null);      

   JPanel gc = new JPanel();       

   JButton btn = new JButton("Example Button");

   btn.setActionCommand("Example Button");

   btn.addFocusListener(this);       

   String[] items = {"London", "Paris", "Madrid", "Moscow", "Wellington"};

   JComboBox cmb = new JComboBox(items);

   cmb.addFocusListener(this);       

   JLabel lbl = new JLabel("Graphical Components:");       

   JTextField tf = new JTextField("A Text Field");

   tf.addFocusListener(this);       

   ta = new JTextArea("Event Feedback..");

   ta.addFocusListener(this);

   JScrollPane st = new JScrollPane(ta);      

   gc.add(lbl);

   gc.add(cmb);

   gc.add(tf);

   gc.add(btn);

   f.add(gc,BorderLayout.NORTH);

   f.add(st, BorderLayout.CENTER);       

   f.setVisible(true);       

  }  

   @Override

   public void focusGained(FocusEvent e) {

   ta.append(e.getComponent().getClass().getName() + " has got the focus\n");

 }   

  @Override

  public void focusLost(FocusEvent e) {

  ta.append(e.getComponent().getClass().getName() + " has lost the focus\n");

 }

}


6.jpg

9.jpg


 

Step 7

 

Now go to "FocusLisetnerDemo.java" and right-click on that, click on "Run" from the menu bar as in the following:

 

7.jpg

 

Output

 

The output shows that java.swing,JComboBox has gotten the focus.

 

8.jpg