Write a java program to demonstrate FocusEvent in Java AWT

 Aim: Wap to demonstrate the FocusEvnet using textfield and one Button on Frame or Applet 

Theory : 

Focus Event: 

                        This is the lowest component in the awt hierarchy. FocusEvents use when user wants to see, where they actually working. Basically, we can give any operation in this method. FocusEvent shines when the user is working on something and lost the working component or directory, then FocusGained and FocusLost methods works efficiently.

To implement this listener in your program, We should implement FocusEvent at the time of class Declaration. For example :

public class FocusDemo extends Frame implements FocusEvent  

As we learn earlier FocusEvent have two Override Methods : 

1) FocusGained: The object of this method invokes when Focus is Gained by some Elements such as Button,TextField, Checkbox, and all other components. 

Syntax : FocusGained(FocusEvent fe)

{//code here, which will work after Focus Gained by any elements }

2) FocusLost: The object of this method invokes when Focus is Lost by some elements such as Button,TextField, CheckBox, Choice, and all other components except Label. Because Label does not have any Invoking method.

Synta : FocusLost(FocusEvent fe)


Note: If we want to use FocusEvent, Then we must override both FocusGained() and FocusLost() Method in our program, And if we only override a single Method such as foucusGained() then our program won't work. and if you don't want any 1 between these two methods then simply make that method empty like 

FocusLost(FocusEvent fe) 

{

//Don't write any code here, to make it empty

}


Here is a program that demonstrates Focus Event :

=================================================================

import java.awt.*;

import java.awt.event.*;



public class FocusDemo1 extends Frame implements FocusListener

{


Label lb1,lb2;

TextField tf1,tf2,tf3;

Button btn1;


public FocusDemo1()

{

setLayout(null);


tf1 = new TextField();

tf1.setBounds(100,50,180,25);


tf2 = new TextField();

tf2.setBounds(100,100,180,25);


tf3 = new TextField();

tf3.setBounds(100,150,180,25);


btn1 = new Button("Verify");

btn1.setBounds(115,200,150,25);


lb1 = new Label();

lb1.setBounds(130,240,200,25);


lb2 = new Label();

lb2.setBounds(130,270,200,25);



add(tf1);add(tf2);add(tf3);add(lb1);add(lb2);add(btn1);


tf1.addFocusListener(this);

tf2.addFocusListener(this);

tf3.addFocusListener(this);

btn1.addFocusListener(this);

}

public void focusGained(FocusEvent fe)

{

String s1 = "tf1";

String s2 = "tf2";

String s3 = "tf3";

String s4 = "tf4";

if(fe.getSource() == tf1)

{

lb1.setText("Focus is Gained By :  "+s1);

if(fe.getSource() == tf2)

{

lb1.setText("Focus is Gained By :  "+s2);

}

if(fe.getSource() == tf3)

{

lb1.setText("Focus is Gained By :  "+s3);

}

if(fe.getSource() == btn1)

{

lb1.setText("Focus is Gained By :  "+s4);

}


}

public void focusLost(FocusEvent fe)

{

String s1 = "tf1";

String s2 = "tf2";

String s3 = "tf3";

String s4 = "tf4";

if(fe.getSource() == tf1)

{

lb2.setText("Focus is Lost By :  "+s1);

if(fe.getSource() == tf2)

{

lb2.setText("Focus is Lost By :  "+s2);

}

if(fe.getSource() == tf3)

{

lb2.setText("Focus is Lost By :  "+s3);

}

if(fe.getSource() == btn1)

{

lb2.setText("Focus is Lost By :  "+s4);

}


}


public static void main(String[] args) {

FocusDemo1 fd1 = new FocusDemo1();

fd1.setVisible(true);

fd1.setTitle("Demonstrating Focus Events");

fd1.setSize(400,400);

}

}

========================================================================

Output :-


إرسال تعليق (0)
أحدث أقدم