Using WindowFocusListener for AWT Frame

The following example illustrates use of WindowFocusEvent with AWT Frame.

import java.awt.*;
import java.awt.event.*;
class FrameFocusEvent extends Frame implements WindowFocusListener
{
    public FrameFocusEvent()
    {
        createAndShowGUI();
    }
   
    private void createAndShowGUI()
    {
        setTitle("Frame with FocusListener demo");
        setLayout(new FlowLayout());
       
        // Add window focus listener
        addWindowFocusListener(this);
       
        setSize(400,400);
        setVisible(true);
    }
   
    public void windowGainedFocus(WindowEvent we)
    {
        setBackground(Color.WHITE);
    }
   
    public void windowLostFocus(WindowEvent we)
    {
        setBackground(Color.LIGHT_GRAY);
    }
   
    public static void main(String args[])
    {
        new FrameFocusEvent();
    }
}

FrameFocusEvent(): Code illustrating WindowFocusListener with AWT Frame is written here.
new FrameFocusEvent(): Create object for the class FrameFocusEvent

Using WindowFocusListener for AWT Frame

Next: Using AdjustmentListener for AWT Scrollbar
Previous: Using ComponentListener for AWT Button

No comments: