Handling Focus Changes - Java Swing

Java examples for Swing:Focus Event

Description

Handling Focus Changes

Demo Code

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.JButton;

public class Main {
  public void m() {
    JButton component = new JButton();
    component.addFocusListener(new MyFocusListener());
  }//from   ww w.j a  v  a2 s.com
}

class MyFocusListener extends FocusAdapter {
  public void focusGained(FocusEvent evt) {
    // The component gained the focus
  }

  public void focusLost(FocusEvent evt) {
    // The component lost the focus
  }
}

Related Tutorials