Use Focus Events in Swing : FocusListener « Swing Event « Java Tutorial






import java.awt.Dimension;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class Main extends JPanel implements FocusListener {
  public Main() {
    JTextField textField = new JTextField("A TextField");
    textField.addFocusListener(this);
    JLabel label = new JLabel("A Label");
    label.addFocusListener(this);
    add(label);

    String comboPrefix = "ComboBox Item #";
    final int numItems = 15;
    Vector vector = new Vector(numItems);
    for (int i = 0; i < numItems; i++) {
      vector.addElement(comboPrefix + i);
    }
    JComboBox comboBox = new JComboBox(vector);
    comboBox.addFocusListener(this);
    add(comboBox);

    JButton button = new JButton("A Button");
    button.addFocusListener(this);
    add(button);

    String listPrefix = "List Item #";
    Vector listVector = new Vector(numItems);
    for (int i = 0; i < numItems; i++) {
      listVector.addElement(listPrefix + i);
    }
    JList list = new JList(listVector);
    list.setSelectedIndex(1); 
    list.addFocusListener(this);
    JScrollPane listScrollPane = new JScrollPane(list);

    listScrollPane.getVerticalScrollBar().setFocusable(false);
    listScrollPane.getHorizontalScrollBar().setFocusable(false);
    add(listScrollPane);

    setPreferredSize(new Dimension(450, 450));
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
  }
  void displayMessage(String prefix, FocusEvent e) {
    System.out.println(prefix
        + (e.isTemporary() ? " (temporary):" : ":")
        + e.getComponent().getClass().getName()
        + "; Opposite component: "
        + (e.getOppositeComponent() != null ? e.getOppositeComponent().getClass().getName()
            : "null") );
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("FocusEventDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JComponent newContentPane = new Main();
    newContentPane.setOpaque(true); // content panes must be opaque
    frame.setContentPane(newContentPane);
    frame.pack();
    frame.setVisible(true);

  }
  public void focusGained(FocusEvent e) {
    displayMessage("Focus gained", e);
}

public void focusLost(FocusEvent e) {
    displayMessage("Focus lost", e);
}
}








15.16.FocusListener
15.16.1.The FocusListener Interface: respond to a component gaining or losing the focus
15.16.2.The event IDs defined in the FocusEvent class
15.16.3.How to Write a Focus ListenerHow to Write a Focus Listener
15.16.4.FocusListener and FocusEvent
15.16.5.Validating a JTextField When Permanently Losing the Focus
15.16.6.Use Focus Events in Swing
15.16.7.Validate a value on the lostFocus event