Java Swing Tutorial - Java FocusAdapter .focusLost (FocusEvent e)








Syntax

FocusAdapter.focusLost(FocusEvent e) has the following syntax.

public void focusLost(FocusEvent e)

Example

In the following code shows how to use FocusAdapter.focusLost(FocusEvent e) method.

/*  w w  w  .  j  av  a 2s .c om*/

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

import javax.swing.JButton;
import javax.swing.JFrame;

public class Main {
  public static void main(String[] argv) throws Exception {
    JButton component = new JButton("a");
    component.addFocusListener(new MyFocusListener());

    
    JFrame f = new JFrame();
    f.add(component);
    f.pack();
    f.setVisible(true);
    
    
  }
}

class MyFocusListener extends FocusAdapter {
  public void focusGained(FocusEvent evt) {
    System.out.println("gained the focus.");
  }

  public void focusLost(FocusEvent evt) {
    System.out.println("lost the focus.");
    boolean isTemporary = evt.isTemporary();
  }
}