Determining the Opposite Component of a Focus Event

The opposite component is the other component affected in a focus event. Specifically, in a focus-lost event, the opposite component is the one gaining the focus. In a focus-gain event, the opposite component is the one losing the focus.

Sometimes the opposite component is null, which indicates that the component is in some other application.

component.addFocusListener(new MyFocusListener()); public class MyFocusListener extends FocusAdapter { public void focusGained(FocusEvent evt) { // The component that lost the focus Component c = evt.getOppositeComponent(); } public void focusLost(FocusEvent evt) { // The component that gained the focus Component c = evt.getOppositeComponent(); } }

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.