Introduction

The interfaceFocusListener is used for receiving keyboard focus events. The class that process focus events needs to implements this interface.

Class declaration

Following is the declaration for java.awt.event.FocusListener interface:

  1. public interface FocusListener
  2. extends EventListener

Interface methods

S.N.Method & Description
1 void focusGained(FocusEvent e) Invoked when a component gains the keyboard focus.
2 void focusLost(FocusEvent e) Invoked when a component loses the keyboard focus.

Methods inherited

This class inherits methods from the following interfaces:

  • java.awt.event.EventListener

FocusListener Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui > AwtListenerDemo.java

  1. package com.tutorialspoint.gui;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class AwtListenerDemo {
  7. private Frame mainFrame;
  8. private Label headerLabel;
  9. private Label statusLabel;
  10. private Panel controlPanel;
  11.  
  12. public AwtListenerDemo(){
  13. prepareGUI();
  14. }
  15.  
  16. public static void main(String[] args){
  17. AwtListenerDemo awtListenerDemo = new AwtListenerDemo();
  18. awtListenerDemo.showFocusListenerDemo();
  19. }
  20.  
  21. private void prepareGUI(){
  22. mainFrame = new Frame("Java AWT Examples");
  23. mainFrame.setSize(400,400);
  24. mainFrame.setLayout(new GridLayout(3, 1));
  25. mainFrame.addWindowListener(new WindowAdapter() {
  26. public void windowClosing(WindowEvent windowEvent){
  27. System.exit(0);
  28. }
  29. });
  30.  
  31. headerLabel = new Label();
  32. headerLabel.setAlignment(Label.CENTER);
  33. statusLabel = new Label();
  34. statusLabel.setAlignment(Label.CENTER);
  35. statusLabel.setSize(350,100);
  36.  
  37. controlPanel = new Panel();
  38. controlPanel.setLayout(new FlowLayout());
  39.  
  40. mainFrame.add(headerLabel);
  41. mainFrame.add(controlPanel);
  42. mainFrame.add(statusLabel);
  43. mainFrame.setVisible(true);
  44. }
  45.  
  46.  
  47. private void showFocusListenerDemo(){
  48.  
  49. headerLabel.setText("Listener in action: FocusListener");
  50.  
  51. Button okButton = new Button("OK");
  52. Button cancelButton = new Button("Cancel");
  53. okButton.addFocusListener(new CustomFocusListener());
  54. cancelButton.addFocusListener(new CustomFocusListener());
  55.  
  56. controlPanel.add(okButton);
  57. controlPanel.add(cancelButton);
  58. mainFrame.setVisible(true);
  59. }
  60.  
  61. class CustomFocusListener implements FocusListener{
  62. public void focusGained(FocusEvent e) {
  63. statusLabel.setText(statusLabel.getText()
  64. + e.getComponent().getClass().getSimpleName() + " gained focus. ");
  65. }
  66.  
  67. public void focusLost(FocusEvent e) {
  68. statusLabel.setText(statusLabel.getText()
  69. + e.getComponent().getClass().getSimpleName() + " lost focus. ");
  70. }
  71. }
  72. }

Compile the program using command prompt. Go to D:/ > AWT and type the following command.

  1. D:\AWT>javac com\tutorialspoint\gui\AwtListenerDemo.java

If no error comes that means compilation is successful. Run the program using following command.

  1. D:\AWT>java com.tutorialspoint.gui.AwtListenerDemo

Verify the following output

awt_focuslistener.jpg