APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.awt.event

public Class FocusEvent

extends ComponentEvent
Class Inheritance
Known Direct Subclasses
sun.awt.CausedFocusEvent
Imports
java.awt.Component, java.io.ObjectStreamException, .Serial, sun.awt.AWTAccessor, .SunToolkit

A low-level event which indicates that a Component has gained or lost the input focus. This low-level event is generated by a Component (such as a TextField). The event is passed to every FocusListener or FocusAdapter object which registered to receive such events using the Component's addFocusListener method. (FocusAdapter objects implement the FocusListener interface.) Each such listener object gets this FocusEvent when the event occurs.

There are two levels of focus events: permanent and temporary. Permanent focus change events occur when focus is directly moved from one Component to another, such as through a call to requestFocus() or as the user uses the TAB key to traverse Components. Temporary focus change events occur when focus is temporarily lost for a Component as the indirect result of another operation, such as Window deactivation or a Scrollbar drag. In this case, the original focus state will automatically be restored once that operation is finished, or, for the case of Window deactivation, when the Window is reactivated. Both permanent and temporary focus events are delivered using the FOCUS_GAINED and FOCUS_LOST event ids; the level may be distinguished in the event using the isTemporary() method.

Every FocusEvent records its cause - the reason why this event was generated. The cause is assigned during the focus event creation and may be retrieved by calling getCause.

An unspecified behavior will be caused if the id parameter of any particular FocusEvent instance is not in the range from FOCUS_FIRST to FOCUS_LAST.

Authors
Carl Quinn, Amy Fowler
Since
1.1
See Also
FocusAdapter, FocusListener, Tutorial: Writing a Focus Listener

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
FocusEvent.Cause

This enum represents the cause of a FocusEvent- the reason why it occurred.

Field Summary

Modifier and TypeField and Description
private final FocusEvent.Cause
cause

A focus event has the reason why this event was generated.

public static final int
FOCUS_FIRST

The first number in the range of ids used for focus events.

public static final int
FOCUS_GAINED

This event indicates that the Component is now the focus owner.

public static final int
FOCUS_LAST

The last number in the range of ids used for focus events.

public static final int
FOCUS_LOST

This event indicates that the Component is no longer the focus owner.

pack-priv transient Component
opposite

The other Component involved in this focus change.

private static final long
serialVersionUID

Hides java.awt.event.ComponentEvent.serialVersionUID.

Use serialVersionUID from JDK 1.1 for interoperability.
pack-priv boolean
temporary

A focus event can have two different levels, permanent and temporary.

Inherited from java.awt.event.ComponentEvent:
COMPONENT_FIRSTCOMPONENT_HIDDENCOMPONENT_LASTCOMPONENT_MOVEDCOMPONENT_RESIZEDCOMPONENT_SHOWN

Constructor Summary

AccessConstructor and Description
public
FocusEvent(Component
The Component that originated the event
source
,
int
An integer indicating the type of event. For information on allowable values, see the class description for FocusEvent
id
,
boolean
Equals true if the focus change is temporary; false otherwise
temporary
,
Component
The other Component involved in the focus change, or null
opposite
)

Constructs a FocusEvent object with the specified temporary state, opposite Component and the Cause.UNKNOWN cause.

public
FocusEvent(Component
The Component that originated the event
source
,
int
An integer indicating the type of event. For information on allowable values, see the class description for FocusEvent
id
,
boolean
Equals true if the focus change is temporary; false otherwise
temporary
,
Component
The other Component involved in the focus change, or null
opposite
,
FocusEvent.Cause
The focus event cause.
cause
)

Constructs a FocusEvent object with the specified temporary state, opposite Component and the cause.

public
FocusEvent(Component
The Component that originated the event
source
,
int
An integer indicating the type of event. For information on allowable values, see the class description for FocusEvent
id
,
boolean
Equals true if the focus change is temporary; false otherwise
temporary
)

Constructs a FocusEvent object and identifies whether or not the change is temporary.

public
FocusEvent(Component
The Component that originated the event
source
,
int
An integer indicating the type of event. For information on allowable values, see the class description for FocusEvent
id
)

Constructs a FocusEvent object and identifies it as a permanent change in focus.

Method Summary

Modifier and TypeMethod and Description
public final FocusEvent.Cause

Returns:

one of Cause values
getCause
()

Returns the event cause.

public Component

Returns:

the other Component involved in the focus change, or null
getOppositeComponent
()

Returns the other Component involved in this focus change.

public boolean

Returns:

true if the focus change is temporary; false otherwise
isTemporary
()

Identifies the focus change event as temporary or permanent.

public String

Returns:

a string identifying the event and its attributes
paramString
()

Overrides java.awt.event.ComponentEvent.paramString.

Returns a parameter string identifying this event.

pack-priv Object

Returns:

a newly created object from deserialized data
readResolve
()

Checks if this deserialized FocusEvent instance is compatible with the current specification which implies that focus event has non-null cause value.

Inherited from java.awt.event.ComponentEvent:
getComponent

Field Detail

causeback to summary
private final FocusEvent.Cause cause

A focus event has the reason why this event was generated. The cause is set during the focus event creation.

Since
9
See Also
getCause()
FOCUS_FIRSTback to summary
public static final int FOCUS_FIRST

The first number in the range of ids used for focus events.

FOCUS_GAINEDback to summary
public static final int FOCUS_GAINED

This event indicates that the Component is now the focus owner.

FOCUS_LASTback to summary
public static final int FOCUS_LAST

The last number in the range of ids used for focus events.

FOCUS_LOSTback to summary
public static final int FOCUS_LOST

This event indicates that the Component is no longer the focus owner.

oppositeback to summary
pack-priv transient Component opposite

The other Component involved in this focus change. For a FOCUS_GAINED event, this is the Component that lost focus. For a FOCUS_LOST event, this is the Component that gained focus. If this focus change occurs with a native application, a Java application in a different VM, or with no other Component, then the opposite Component is null.

Since
1.4
See Also
getOppositeComponent
serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.awt.event.ComponentEvent.serialVersionUID.

Use serialVersionUID from JDK 1.1 for interoperability.

Annotations
@Serial
temporaryback to summary
pack-priv boolean temporary

A focus event can have two different levels, permanent and temporary. It will be set to true if some operation takes away the focus temporarily and intends on getting it back once the event is completed. Otherwise it will be set to false.

See Also
isTemporary

Constructor Detail

FocusEventback to summary
public FocusEvent(Component source, int id, boolean temporary, Component opposite)

Constructs a FocusEvent object with the specified temporary state, opposite Component and the Cause.UNKNOWN cause. The opposite Component is the other Component involved in this focus change. For a FOCUS_GAINED event, this is the Component that lost focus. For a FOCUS_LOST event, this is the Component that gained focus. If this focus change occurs with a native application, with a Java application in a different VM, or with no other Component, then the opposite Component is null.

This method throws an IllegalArgumentException if source is null.

Parameters
source:Component

The Component that originated the event

id:int

An integer indicating the type of event. For information on allowable values, see the class description for FocusEvent

temporary:boolean

Equals true if the focus change is temporary; false otherwise

opposite:Component

The other Component involved in the focus change, or null

Exceptions
IllegalArgumentException:
if source equals null
Since
1.4
See Also
getSource(), getID(), isTemporary(), getOppositeComponent(), Cause#UNKNOWN
FocusEventback to summary
public FocusEvent(Component source, int id, boolean temporary, Component opposite, FocusEvent.Cause cause)

Constructs a FocusEvent object with the specified temporary state, opposite Component and the cause. The opposite Component is the other Component involved in this focus change. For a FOCUS_GAINED event, this is the Component that lost focus. For a FOCUS_LOST event, this is the Component that gained focus. If this focus change occurs with a native application, with a Java application in a different VM, or with no other Component, then the opposite Component is null.

This method throws an IllegalArgumentException if source or cause is null.

Parameters
source:Component

The Component that originated the event

id:int

An integer indicating the type of event. For information on allowable values, see the class description for FocusEvent

temporary:boolean

Equals true if the focus change is temporary; false otherwise

opposite:Component

The other Component involved in the focus change, or null

cause:FocusEvent.Cause

The focus event cause.

Exceptions
IllegalArgumentException:
if source equals null or if cause equals null
Since
9
See Also
getSource(), getID(), isTemporary(), getOppositeComponent(), Cause
FocusEventback to summary
public FocusEvent(Component source, int id, boolean temporary)

Constructs a FocusEvent object and identifies whether or not the change is temporary.

This method throws an IllegalArgumentException if source is null.

Parameters
source:Component

The Component that originated the event

id:int

An integer indicating the type of event. For information on allowable values, see the class description for FocusEvent

temporary:boolean

Equals true if the focus change is temporary; false otherwise

Exceptions
IllegalArgumentException:
if source equals null
See Also
getSource(), getID(), isTemporary()
FocusEventback to summary
public FocusEvent(Component source, int id)

Constructs a FocusEvent object and identifies it as a permanent change in focus.

This method throws an IllegalArgumentException if source is null.

Parameters
source:Component

The Component that originated the event

id:int

An integer indicating the type of event. For information on allowable values, see the class description for FocusEvent

Exceptions
IllegalArgumentException:
if source equals null
See Also
getSource(), getID()

Method Detail

getCauseback to summary
public final FocusEvent.Cause getCause()

Returns the event cause.

Returns:FocusEvent.Cause

one of Cause values

Since
9
getOppositeComponentback to summary
public Component getOppositeComponent()

Returns the other Component involved in this focus change. For a FOCUS_GAINED event, this is the Component that lost focus. For a FOCUS_LOST event, this is the Component that gained focus. If this focus change occurs with a native application, with a Java application in a different VM, or with no other Component, then null is returned.

Returns:Component

the other Component involved in the focus change, or null

Since
1.4
isTemporaryback to summary
public boolean isTemporary()

Identifies the focus change event as temporary or permanent.

Returns:boolean

true if the focus change is temporary; false otherwise

paramStringback to summary
public String paramString()

Overrides java.awt.event.ComponentEvent.paramString.

Returns a parameter string identifying this event. This method is useful for event-logging and for debugging.

Returns:String

a string identifying the event and its attributes

readResolveback to summary
pack-priv Object readResolve() throws ObjectStreamException

Checks if this deserialized FocusEvent instance is compatible with the current specification which implies that focus event has non-null cause value. If the check fails a new FocusEvent instance is returned which cause field equals to Cause#UNKNOWN and its other fields have the same values as in this FocusEvent instance.

Returns:Object

a newly created object from deserialized data

Annotations
@Serial
Exceptions
ObjectStreamException:
if a new object replacing this object could not be created
Since
9
See Also
cause
java.awt.event back to summary

public final Enum FocusEvent.Cause

extends Enum<FocusEvent.Cause>
Class Inheritance

This enum represents the cause of a FocusEvent- the reason why it occurred. Possible reasons include mouse events, keyboard focus traversal, window activation. If no cause is provided then the reason is UNKNOWN.
Since
9

Field Summary

Modifier and TypeField and Description
public static final FocusEvent.Cause
ACTIVATION

An activation of a toplevel window.

public static final FocusEvent.Cause
CLEAR_GLOBAL_FOCUS_OWNER

Clearing global focus owner.

public static final FocusEvent.Cause
MOUSE_EVENT

An activating mouse event.

public static final FocusEvent.Cause
ROLLBACK

Restoring focus after a focus request has been rejected.

public static final FocusEvent.Cause
TRAVERSAL

A focus traversal action with unspecified direction.

public static final FocusEvent.Cause
TRAVERSAL_BACKWARD

A backward focus traversal action.

public static final FocusEvent.Cause
TRAVERSAL_DOWN

A down-cycle focus traversal action.

public static final FocusEvent.Cause
TRAVERSAL_FORWARD

A forward focus traversal action.

public static final FocusEvent.Cause
TRAVERSAL_UP

An up-cycle focus traversal action.

public static final FocusEvent.Cause
UNEXPECTED

A system action causing an unexpected focus change.

public static final FocusEvent.Cause
UNKNOWN

The default value.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static FocusEvent.Cause
public static FocusEvent.Cause[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

ACTIVATIONback to summary
public static final FocusEvent.Cause ACTIVATION

An activation of a toplevel window.

CLEAR_GLOBAL_FOCUS_OWNERback to summary
public static final FocusEvent.Cause CLEAR_GLOBAL_FOCUS_OWNER

Clearing global focus owner.

MOUSE_EVENTback to summary
public static final FocusEvent.Cause MOUSE_EVENT

An activating mouse event.

ROLLBACKback to summary
public static final FocusEvent.Cause ROLLBACK

Restoring focus after a focus request has been rejected.

TRAVERSALback to summary
public static final FocusEvent.Cause TRAVERSAL

A focus traversal action with unspecified direction.

TRAVERSAL_BACKWARDback to summary
public static final FocusEvent.Cause TRAVERSAL_BACKWARD

A backward focus traversal action.

TRAVERSAL_DOWNback to summary
public static final FocusEvent.Cause TRAVERSAL_DOWN

A down-cycle focus traversal action.

TRAVERSAL_FORWARDback to summary
public static final FocusEvent.Cause TRAVERSAL_FORWARD

A forward focus traversal action.

TRAVERSAL_UPback to summary
public static final FocusEvent.Cause TRAVERSAL_UP

An up-cycle focus traversal action.

UNEXPECTEDback to summary
public static final FocusEvent.Cause UNEXPECTED

A system action causing an unexpected focus change.

UNKNOWNback to summary
public static final FocusEvent.Cause UNKNOWN

The default value.

Constructor Detail

Causeback to summary
private Cause()

Method Detail

valueOfback to summary
public static FocusEvent.Cause valueOf(String name)
valuesback to summary
public static FocusEvent.Cause[] values()