FocusEvent class and FocusListener interface in java
//FocusEvent class and FocusListener interface
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/*
<applet code = "FocusEvent_and_FocusListener"
width = 200 height = 200>
</applet>
*/
public class FocusEvent_and_FocusListener
extends JApplet implements FocusListener
{
Container con;
JTextField txt1, txt2, txt3;
public void init()
{
con = getContentPane();
con.setLayout(new FlowLayout());
txt1 = new JTextField(20);
txt1.addFocusListener(this);
con.add(txt1);
txt2 = new JTextField(20);
txt2.addFocusListener(this);
con.add(txt2);
txt3 = new JTextField(20);
txt3.addFocusListener(this);
con.add(txt3);
}
public void focusGained(FocusEvent fe)
{
if(fe.getSource() == txt1)
txt1.setBackground(Color.yellow);
else if(fe.getSource() == txt2)
txt2.setBackground(Color.yellow);
else if(fe.getSource() == txt3)
txt3.setBackground(Color.yellow);
}
public void focusLost(FocusEvent fe)
{
if(fe.getSource() == txt1)
txt1.setBackground(Color.white);
else if(fe.getSource() == txt2)
txt2.setBackground(Color.white);
else if(fe.getSource() == txt3)
txt3.setBackground(Color.white);
}
}
OUTOUT:
Comments
Post a Comment