Focus Event in Java with Examples - Beginning With Programming

Focus Event in Java with Examples

Table of Contents

a) Introduction
b) Different Method of Focus Event
c) Example with Source code
d) Output

*********************

Introduction

Focus Event is the low-level event which indicates the focus gained or focus lost from the keyboard. It is generated by the components such as text field. The event generated is passed through every FocusListener or FocusAdapter objects which is registered by addFocusListener to receive the event.

The methods of the Focus Event are:

public void focusGained(FocusEvent e)
public void focusLost(FocusEvent e)

Let us see the example of Focus Event using FocusListener;

To use the FocusListener interface we have to implement it to the main class as shown in below code.

Source Code:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package EventHandling;

/**
 *
 * @author AnkitPC
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FocusEventDemo implements FocusListener{
    JFrame f1;
    JTextField t1, t2;
    FocusEventDemo(){
        f1= new JFrame();
        f1.setSize(300, 300); //size of frame
        f1.setLayout(new FlowLayout()); //setting layout
       
        f1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
       
        t1= new JTextField(20);
        t2= new JTextField(20);
        //adding component to the frame
        f1.add(t1);
        f1.add(t2);
        //registering the event
        t1.addFocusListener(this);
        t2.addFocusListener(this);
       
        f1.setVisible(true);
    }
    public static void main(String[] args) {
        new FocusEventDemo(); //calling constructor
    }
   
    public void focusGained(FocusEvent e){
        if(e.getSource()==t1){
            t1.setBackground(Color.cyan);
        }
        else if(e.getSource()==t2){
            t2.setBackground(Color.cyan);
        }
    }
    public void focusLost(FocusEvent e){
        if(e.getSource()==t1){
            t1.setBackground(Color.red);
        }
        else if(e.getSource()==t2){
            t2.setBackground(Color.red);
        }
    }
}

If we use Listener interface than we have to override the all methods that is in Focus Listener. As you can see that there are only two methods of focus event so we override both. In the source code you can see that when focus is gained then it shows the color cyan and if focus is lost then it shows the color red.

Output:

In this figure above, focus is gained by the text field first and focus is lost in text field second.


Now in the figure above you can see the focus is gained by the second text field and lost by first text field.

Also let us see the example of Focus Event using FocusAdpater;

To use FocusAdapter we extend the Adapter class to the main class as shown in the code below.

Source Code:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package EventHandling;

/**
 *
 * @author AnkitPC
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FocusEventDemo extends FocusAdapter{
    JFrame f1;
    JTextField t1, t2;
    FocusEventDemo(){
        f1= new JFrame();
        f1.setSize(300, 300); //size of frame
        f1.setLayout(new FlowLayout()); //setting layout
       
        f1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
       
        t1= new JTextField(20);
        t2= new JTextField(20);
        //adding component to the frame
        f1.add(t1);
        f1.add(t2);
        //registering the event
        t1.addFocusListener(this);
        t2.addFocusListener(this);
       
        f1.setVisible(true);
    }
    public static void main(String[] args) {
        new FocusEventDemo(); //calling constructor
    }
   
    public void focusGained(FocusEvent e){
        if(e.getSource()==t1){
            t1.setBackground(Color.cyan);
        }
        else if(e.getSource()==t2){
            t2.setBackground(Color.cyan);
        }
    }
   
}

Now in the source code above you can see that I have removed the focusLost method. But also the code is working, So the main reason of using the Adapter class over Listener interface is that we can override the method which is required.

Output:



In the above code i have removed the method focusLost so it only shows the focus gained by the textfield.

*****************

Post a Comment

0 Comments