Monday, August 5, 2013

FocusListener

FocusListener is an interface to receive focus events. The focus events can be generated by a component when the focus is gained or lost.

An application that wants to receive the focus events must implement the FocusListener interface and register the listener with the component by using the addFocusListener(FocusListener listener) method. The FocusListener interface has two methods that need to be overridden. These methods are focusGained(FocusEvent e) and focusLost(FocusEvent e). See the example code shown below.

In the example below, there are two text fields. One allows the user to input the user name and another one is for inputting the password. The setEchoChar(char ch) method is used to specify the character to display (instead of the original character) when the use types password in the password text field. All text fields are registered to receive the focus events. Since, it is required that the user input the use name and password before leaving the text fields, the code has to be written in the focusLost method to detect whether the user name or password field is blank. If it is blank, the label next to the field will show "User name can not be blank" or "Password can not be blank" in red color. The setForeground(Color c) method is used to set the text color of the label.

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Color;
import java.awt.Button;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class InterfaceShow extends Frame{
private Label lblpass;
private Label lblusername;
private TextField txtusername;
private TextField txtpass;
InterfaceShow(String title){
setTitle(title);
setLayout(new GridLayout(3,3));
setSize(new Dimension(500,150));
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
txtusername=new TextField(30);
txtusername.addFocusListener(new FocusList());
add(new Label("Your name:"));
add(txtusername);
lblusername=new Label();
add(lblusername);
txtpass=new TextField(30);
txtpass.setEchoChar('*');
txtpass.addFocusListener(new FocusList());
add(new Label("Your password:"));
add(txtpass);
lblpass=new Label();
add(lblpass);
add(new Button("OK"));
setVisible(true);
validate();
}
class FocusList implements FocusListener{
public void focusGained(FocusEvent e){

}
public void focusLost(FocusEvent e){
TextField txt=(TextField)e.getSource();
if(txt.getText().equals("") && txt==txtusername){
lblusername.setForeground(Color.RED);
lblusername.setText("User name can not be blank.");
}
else
lblusername.setText("");

if(txt.getText().equals("") && txt==txtpass){
lblpass.setForeground(Color.RED);
lblpass.setText("Password can not be blank.");
}
else
lblpass.setText("");
}


}


}
public class FocusListenerTest {
public static void main(String[] args){
new InterfaceShow("FocusListener");

}
}

focuslistener focusevent

No comments:

Post a Comment