UsingFocusListener.java Source code

Java tutorial

Introduction

Here is the source code for UsingFocusListener.java

Source

import java.awt.KeyboardFocusManager;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class UsingFocusListener {

    public static void main(String[] a) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTextField textField = new JTextField("A TextField");

        KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        focusManager.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent e) {
                String prop = e.getPropertyName();
                System.out.println(prop);

            }
        });

        frame.add(textField, "North");
        frame.add(new JTextField(), "South");
        frame.setSize(300, 200);
        frame.setVisible(true);
    }

}