The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.
See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.
Focus events are fired whenever a component gains or loses the keyboard focus. This is true whether the change in focus occurs through the mouse, the keyboard, or programmatically. To get familiar with basic focus concepts or to obtain detailed information about focus, see How to Use the Focus Subsystem.
This section explains how to get focus events for a particular component by registering a FocusListener
instance on it. To get focus for a window only, implement a WindowFocusListener
instance instead. To obtain the focus status of many components, consider implementing a PropertyChangeListener
instance on the KeyboardFocusManager
class, as described in
Tracking Focus Changes to Multiple Components in
How to Use the Focus Subsystem.
The following example demonstrates focus events. The window displays a variety of components. A focus listener, registered on each component, reports every focus-gained and focus-lost event. For each event, the other component involved in the focus change, the opposite component, is reported. For example, when the focus goes from a button to a text field, a focus-lost event is fired by the button (with the text field as the opposite component) and then a focus-gained event is fired by the text field (with the button as the opposite component). Focus-lost as well as focus-gained events can be temporary. For example, a temporary focus-lost event occurs when the window loses the focus. A temporary focus-gained event occurs on popup menus.
setRequestFocusEnabled(false)
.setRequestFocusEnabled(false)
on the text area. The demo could use setFocusable(false)
to truly remove the text area from the focus cycle, but that would have the unfortunate effect of making the component unavailable to those who use assistive technologies.The complete code for this demo is in the
FocusEventDemo.java
file. The following code snippet represents the focus-event handling mechanism:
public class FocusEventDemo ... implements FocusListener ... { public FocusEventDemo() { ... JTextField textField = new JTextField("A TextField"); textField.addFocusListener(this); ... JLabel label = new JLabel("A Label"); label.addFocusListener(this); ... JComboBox comboBox = new JComboBox(vector); comboBox.addFocusListener(this); ... JButton button = new JButton("A Button"); button.addFocusListener(this); ... JList list = new JList(listVector); list.setSelectedIndex(1); //It's easier to see the focus change //if an item is selected. list.addFocusListener(this); JScrollPane listScrollPane = new JScrollPane(list); ... //Set up the area that reports focus-gained and focus-lost events. display = new JTextArea(); display.setEditable(false); //The method setRequestFocusEnabled prevents a //component from being clickable, but it can still //get the focus through the keyboard - this ensures //user accessibility. display.setRequestFocusEnabled(false); display.addFocusListener(this); JScrollPane displayScrollPane = new JScrollPane(display); ... } ... public void focusGained(FocusEvent e) { displayMessage("Focus gained", e); } public void focusLost(FocusEvent e) { displayMessage("Focus lost", e); } void displayMessage(String prefix, FocusEvent e) { display.append(prefix + (e.isTemporary() ? " (temporary):" : ":") + e.getComponent().getClass().getName() + "; Opposite component: " + (e.getOppositeComponent() != null ? e.getOppositeComponent().getClass().getName() : "null") + newline); } ... }
The corresponding adapter class is
FocusAdapter
.
Method | Purpose |
---|---|
focusGained(FocusEvent) | Called just after the listened-to component gets the focus. |
focusLost(FocusEvent) | Called just after the listened-to component loses the focus. |
Method | Purpose |
---|---|
boolean isTemporary() | Returns the true value if a focus-lost or focus-gained event is temporary. |
Component getComponent() (in java.awt.event.ComponentEvent ) |
Returns the component that fired the focus event. |
Component getOppositeComponent() | Returns the other component involved in the focus change. For a FOCUS_GAINED event, this is the component that lost the focus. For a FOCUS_LOST event, this is the component that gained the focus. If the focus change involves a native application, a Java application in a different VM or context, or no other component, then null is returned. |
The following table lists the examples that use focus listeners.
Example | Where Described | Notes |
---|---|---|
FocusEventDemo |
This section | Reports all focus events that occur on several components to demonstrate the circumstances under which focus events are fired. |
TrackFocusDemo |
How to Use the Focus Subsystem | The custom component,
Picture , implements a focus listener to draw a red border around the component when it is the current focus owner. |