ADS

Using Focuslistener On Awt Button

The following example illustrates use of FocusListener on AWT Button.
 import java.awt.*;
import java.awt.event.*;
class ButtonFocusEvent extends Frame implements FocusListener
{
Button b1,b2;

    public ButtonFocusEvent()
    {
        createAndShowGUI();
    }
   
    private void createAndShowGUI()
    {
        setTitle("FocusListener for Button");
        setLayout(new FlowLayout());
       
        // Create 2 buttons
        b1=new Button("Button 1");
        b2=new Button("Button 2");
           
        // Add them
        add(b1);
        add(b2);
       
        // Add FocusListeners
        b1.addFocusListener(this);
        b2.addFocusListener(this);
       
        setSize(400,400);
        setVisible(true);
    }
   
    public void focusGained(FocusEvent fe)
    {
        // Get what button got focus
        Button b=(Button)fe.getSource();
        b.setForeground(Color.RED);
    }
   
    public void focusLost(FocusEvent fe)
    {
        // Get what button lost focus
        Button b=(Button)fe.getSource();
        b.setForeground(Color.BLACK);
    }
   
    public static void main(String args[])
    {
        new ButtonFocusEvent();
    }
}

ButtonFocusEvent(): Code illustrating FocusListener on AWT Button is written here.
new ButtonFocusEvent(): Create object for ButtonFocusEvent

 The following example illustrates use of FocusListener on AWT Button Using FocusListener on AWT Button

Next: Using ContainerListener on AWT Frame
Previous: Using FocusListener for AWT TextField

Subscribe to receive free email updates:

ADS