The Component
class is the abstract superclass of
the nonmenu-related Abstract Window Toolkit components. Class
Component
can also be extended directly to create a
lightweight component. A lightweight component is a component that is
not associated with a native opaque window.
Serializable
protocol will be saved when
the object is stored. If an AWT object has listeners that
aren't marked serializable, they will be dropped at
writeObject
time. Developers will need, as always,
to consider the implications of making an object serializable.
One situation to watch out for is this:
import java.awt.*; import java.awt.event.*; import java.io.Serializable; class MyApp implements ActionListener, Serializable { BigObjectThatShouldNotBeSerializedWithAButton bigOne; Button aButton = new Button(); MyApp() { // Oops, now aButton has a listener with a reference // to bigOne! aButton.addActionListener(this); } public void actionPerformed(ActionEvent e) { System.out.println("Hello There"); } }In this example, serializing
aButton
by itself
will cause MyApp
and everything it refers to
to be serialized as well. The problem is that the listener
is serializable by coincidence, not by design. To separate
the decisions about MyApp
and the
ActionListener
being serializable one can use a
nested class, as in the following example:
import java.awt.*; import java.awt.event.*; import java.io.Serializable; class MyApp java.io.Serializable { BigObjectThatShouldNotBeSerializedWithAButton bigOne; Button aButton = new Button(); class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("Hello There"); } } MyApp() { aButton.addActionListener(new MyActionListener()); } }
Note: For more information on the paint mechanisms utilitized by AWT and Swing, including information on how to write the most efficient painting code, see Painting in AWT and Swing.
For details on the focus subsystem, see How to Use the Focus Subsystem, a section in The Java Tutorial, and the Focus Specification for more information.
getAlignmentY
.
Specifies an alignment to the bottom of the component.getAlignmentY
and
getAlignmentX
. Specifies an alignment to
the center of the componentgetAlignmentX
.
Specifies an alignment to the left side of the component.getAlignmentX
.
Specifies an alignment to the right side of the component.getAlignmentY()
.
Specifies an alignment to the top of the component.l
is null
,
no exception is thrown and no action is performed.l
is null
,
no exception is thrown and no action is performed.l
is null
,
no exception is thrown and no action is performed.l
is null
,
no exception is thrown and no action is performed.getInputMethodRequests
to return an
InputMethodRequests
instance.
If listener l
is null
,
no exception is thrown and no action is performed.l
is null
,
no exception is thrown and no action is performed.l
is null
,
no exception is thrown and no action is performed.For information on how mouse wheel events are dispatched, see the class description for MouseWheelEvent .
If l is null
, no exception is thrown and no
action is performed.
Component
displayable by connecting it to a
native screen resource.
This method is called internally by the toolkit and should
not be called directly by programs.Component
is inheriting a bound property, then no
event will be fired in response to a change in the inherited property.
If listener
is null
,
no exception is thrown and no action is performed.
Component
is inheriting a bound property, then no
event will be fired in response to a change in the inherited property.
If propertyName
or listener
is null
,
no exception is thrown and no action is taken.
ComponentOrientation
property of this component
and all components contained within it.false
, this Component is inheriting the
Set from an ancestor, or from the current KeyboardFocusManager.
This method does not cause the image to begin loading. An
application must use the prepareImage
method
to force the loading of an image.
Information on the flags returned by this method can be found
with the discussion of the ImageObserver
interface.
This method does not cause the image to begin loading. An
application must use the prepareImage
method
to force the loading of an image.
The checkImage
method of Component
calls its peer's checkImage
method to calculate
the flags. If this component does not yet have a peer, the
component's toolkit's checkImage
method is called
instead.
Information on the flags returned by this method can be found
with the discussion of the ImageObserver
interface.
x
and y
are defined to be
relative to the coordinate system of this component.VolatileImage
interface.processEvent
before returning for 1.1-style
events which have been enabled for the Component
.
The equals
method implements an equivalence relation
on non-null object references:
x
, x.equals(x)
should return
true
.
x
and y
, x.equals(y)
should return true
if and only if
y.equals(x)
returns true
.
x
, y
, and z
, if
x.equals(y)
returns true
and
y.equals(z)
returns true
, then
x.equals(z)
should return true
.
x
and y
, multiple invocations of
x.equals(y) consistently return true
or consistently return false
, provided no
information used in equals
comparisons on the
objects is modified.
x
,
x.equals(null)
should return false
.
The equals method for class Object
implements
the most discriminating possible equivalence relation on objects;
that is, for any non-null reference values x
and
y
, this method returns true
if and only
if x
and y
refer to the same object
(x == y
has the value true
).
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
AccessibleContext
associated
with this Component
.
The method implemented by this base
class returns null. Classes that extend Component
should implement this method to return the
AccessibleContext
associated with the subclass.Rectangle
object. The bounds specify this
component's width, height, and location relative to
its parent.null
a new
Rectangle
is allocated.
This version of getBounds
is useful if the caller
wants to avoid allocating a new Rectangle
object
on the heap.ColorModel
used to display
the component on the output device.
The locate
method of Component
simply
returns the component itself if the (x, y)
coordinate location is inside its bounding box, and null
otherwise.
LayoutManager
and Component
subclasses that wish to respect orientation should call this method to
get the component's orientation before performing layout or drawing.Cursor.DEFAULT_CURSOR
is returned.DropTarget
associated with this
Component
.setFocusTraversalKeys
for a full description of each key.)
If a Set of traversal keys has not been explicitly defined for this Component, then this Component's parent's Set is returned. If no Set has been explicitly defined for any of this Component's ancestors, then the current KeyboardFocusManager's default Set is returned.
null
if this component is currently not
displayable.GraphicsConfiguration
associated with this
Component
.
If the Component
has not been assigned a specific
GraphicsConfiguration
,
the GraphicsConfiguration
of the
Component
object's top-level container is
returned.
If the Component
has been created, but not yet added
to a Container
, this method returns null
.component.getBounds().heightcomponent.getSize().height
because it
doesn't cause any heap allocations.InputMethodRequests
instance.
At the same time, it also has to handle input method events.FooListener
s
upon this Component
.
FooListener
s are registered using the
addFooListener
method.
You can specify the listenerType
argument
with a class literal, such as
FooListener.class
.
For example, you can query a
Component
c
for its mouse listeners with the following code:
MouseListener[] mls = (MouseListener[])(c.getListeners(MouseListener.class));If no such listeners exist, this method returns an empty array.
Due to the asynchronous nature of native event handling, this
method can return outdated values (for instance, after several calls
of setLocation()
in rapid succession). For this
reason, the recommended method of obtaining a component's position is
within java.awt.event.ComponentListener.componentMoved()
,
which is called after the operating system has finished moving the
component.
null
a new
Point
is allocated.
This version of getLocation
is useful if the
caller wants to avoid allocating a new Point
object on the heap.Component
's
coordinate space if the Component
is directly under the mouse
pointer, otherwise returns null
.
If the Component
is not showing on the screen, this method
returns null
even if the mouse pointer is above the area
where the Component
would be displayed.
If the Component
is partially or fully obscured by other
Component
s or native windows, this method returns a non-null
value only if the mouse pointer is located above the unobscured part of the
Component
.
For Container
s it returns a non-null value if the mouse is
above the Container
itself or above any of its descendants.
Use
if you need to exclude children.
Sometimes the exact mouse coordinates are not important, and the only thing
that matters is whether a specific Component
is under the mouse
pointer. If the return value of this method is null
, mouse
pointer is not directly above the Component
.
Dimension
object. The height
field of the Dimension
object contains
this component's height, and the width
field of the Dimension
object contains
this component's width.null
a new
Dimension
object is allocated. This version of
getSize
is useful if the caller wants to avoid
allocating a new Dimension
object on the heap.component.getBounds().width
,
or component.getSize().width
because it
doesn't cause any heap allocations.component.getBounds().x
,
or component.getLocation().x
because it doesn't
cause any heap allocations.component.getBounds().y
,
or component.getLocation().y
because it
doesn't cause any heap allocations.true
if this Component
is the
focus owner. This method is obsolete, and has been replaced by
isFocusOwner()
.java.util.Hashtable
.
The general contract of hashCode
is:
hashCode
method on each of
the two objects must produce the same integer result.
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
This method should return true if further updates are needed or false if the required information has been acquired. The image which was being tracked is passed in using the img argument. Various constants are combined to form the infoflags argument which indicates what information about the image is now available. The interpretation of the x, y, width, and height arguments depends on the contents of the infoflags argument.
The infoflags
argument should be the bitwise inclusive
OR of the following flags: WIDTH
,
HEIGHT
, PROPERTIES
, SOMEBITS
,
FRAMEBITS
, ALLBITS
, ERROR
,
ABORT
.
false
, this Component is
inheriting its background color from an ancestor.false
, this Component is inheriting
its cursor from an ancestor.A component is made displayable either when it is added to a displayable containment hierarchy or when its containment hierarchy is made displayable. A containment hierarchy is made displayable when its ancestor window is either packed or made visible.
A component is made undisplayable either when it is removed from a displayable containment hierarchy or when its containment hierarchy is made undisplayable. A containment hierarchy is made undisplayable when its ancestor window is disposed.
setEnabled
method.true
if this Component
is the
focus owner.Component
can become the focus
owner.false
, this Component is inheriting its
font from an ancestor.false
, this Component is
inheriting its foreground color from an ancestor.Component
and Container
,
other than the ones defined in this package like Button
or Scrollbar
, are lightweight.
All of the Swing components are lightweights.
This method will always return false
if this component
is not displayable because it is impossible to determine the
weight of an undisplayable component.
null
value otherwise returns false.setMinimumSize
has been
invoked with a non-null value.An opaque component paints every pixel within its rectangular region. A non-opaque component paints only some of its pixels, allowing the pixels underneath it to "show through". A component that does not fully paint its pixels therefore provides a degree of transparency. Only lightweight components can be transparent.
Subclasses that guarantee to always completely paint their contents should override this method and return true. All of the "heavyweight" AWT components are opaque.
null
value otherwise returns false.Frame
objects.System.out
.wait
methods.
The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.
This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:
synchronized
statement
that synchronizes on the object.
Class,
by executing a
synchronized static method of that class.
Only one thread at a time can own an object's monitor.
wait
methods.
The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object. The awakened threads will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened threads enjoy no reliable privilege or disadvantage in being the next thread to lock this object.
This method should only be called by a thread that is the owner
of this object's monitor. See the notify
method for a
description of the ways in which a thread can become the owner of
a monitor.
This method is called when the contents of the component should
be painted; such as when the component is first being shown or
is damaged and in need of repair. The clip rectangle in the
Graphics
parameter is set to the area
which needs to be painted.
Subclasses of Component
that override this
method need not call super.paint(g)
.
For performance reasons, Component
s with zero width
or height aren't considered to need painting when they are first shown,
and also aren't considered to need repair.
Note: For more information on the paint mechanisms utilitized by AWT and Swing, including information on how to write the most efficient painting code, see Painting in AWT and Swing.
The origin of the graphics context, its
(0
, 0
) coordinate point, is the
top-left corner of this component. The clipping region of the
graphics context is the bounding rectangle of this component.
The image data is downloaded asynchronously in another thread, and an appropriately scaled screen representation of the image is generated.
The default implementation of this method calls the
paint
method.
The origin of the graphics context, its
(0
, 0
) coordinate point, is the
top-left corner of this component. The clipping region of the
graphics context is the bounding rectangle of this component.
The origin of the graphics context, its
(0
, 0
) coordinate point, is the
top-left corner of this component. The clipping region of the
graphics context is the bounding rectangle of this component.
l
is null
,
no exception is thrown and no action is performed.l
is null
,
no exception is thrown and no action is performed.l
is null
,
no exception is thrown and no action is performed.l
is null
,
no exception is thrown and no action is performed.l
is null
,
no exception is thrown and no action is performed.l
is null
,
no exception is thrown and no action is performed.l
is null
,
no exception is thrown and no action is performed.l
is null
,
no exception is thrown and no action is performed.Component
undisplayable by destroying it native
screen resource.
This method is called by the toolkit internally and should
not be called directly by programs. Code overriding
this method should call super.removeNotify
as
the first line of the overriding method.
If listener is null, no exception is thrown and no action is performed.
PropertyChangeListener
from the listener
list for a specific property. This method should be used to remove
PropertyChangeListener
s
that were registered for a specific bound property.
If propertyName
or listener
is null
,
no exception is thrown and no action is taken.
If this component is a lightweight component, this method
causes a call to this component's paint
method as soon as possible. Otherwise, this method causes
a call to this component's update
method as soon
as possible.
Note: For more information on the paint mechanisms utilitized by AWT and Swing, including information on how to write the most efficient painting code, see Painting in AWT and Swing.
If this component is a lightweight component, this method
causes a call to this component's paint
method
as soon as possible. Otherwise, this method causes a call to
this component's update
method as soon as possible.
Note: For more information on the paint mechanisms utilitized by AWT and Swing, including information on how to write the most efficient painting code, see Painting in AWT and Swing.
paint
within tm
milliseconds.
Note: For more information on the paint mechanisms utilitized by AWT and Swing, including information on how to write the most efficient painting code, see Painting in AWT and Swing.
tm
milliseconds.
If this component is a lightweight component, this method causes
a call to this component's paint
method.
Otherwise, this method causes a call to this component's
update
method.
Note: For more information on the paint mechanisms utilitized by AWT and Swing, including information on how to write the most efficient painting code, see Painting in AWT and Swing.
This method cannot be used to set the focus owner to no Component at
all. Use KeyboardFocusManager.clearGlobalFocusOwner()
instead.
Because the focus behavior of this method is platform-dependent,
developers are strongly encouraged to use
requestFocusInWindow
when possible.
This method returns a boolean value. If false
is returned,
the request is guaranteed to fail. If true
is
returned, the request will succeed unless it is vetoed, or an
extraordinary event, such as disposal of the Component's peer, occurs
before the request can be granted by the native windowing system. Again,
while a return value of true
indicates that the request is
likely to succeed, developers must never assume that this Component is
the focus owner until this Component receives a FOCUS_GAINED event.
This method cannot be used to set the focus owner to no Component at
all. Use KeyboardFocusManager.clearGlobalFocusOwner()
instead.
The focus behavior of this method can be implemented uniformly across
platforms, and thus developers are strongly encouraged to use this
method over requestFocus
when possible. Code which relies
on requestFocus
may exhibit different focus behavior on
different platforms.
The background color affects each component differently and the parts of the component that are affected by the background color may differ between operating systems.
x
and y
, and the
new size is specified by width
and height
.r
. This component's new
position is specified by r.x
and r.y
,
and its new size is specified by r.width
and
r.height
LayoutManager
and Component
subclasses will use this property to
determine how to lay out and draw components.
At construction time, a component's orientation is set to
ComponentOrientation.UNKNOWN
,
indicating that it has not been specified
explicitly. The UNKNOWN orientation behaves the same as
ComponentOrientation.LEFT_TO_RIGHT
.
To set the orientation of a single component, use this method. To set the orientation of an entire component hierarchy, use applyComponentOrientation .
contains
method for
this component returns true for the current cursor location, and
this Component is visible, displayable, and enabled. Setting the
cursor of a Container
causes that cursor to be displayed
within all of the container's subcomponents, except for those
that have a non-null
cursor.DropTarget
with this component.
The Component
will receive drops only if it
is enabled.b
. An enabled component can respond to user
input and generate events. Components are enabled initially by default.
Note: Disabling a lightweight component does not prevent it from receiving MouseEvents.
The default values for a Component's focus traversal keys are implementation-dependent. Sun recommends that all implementations for a particular native platform use the same default values. The recommendations for Windows and Unix are listed below. These recommendations are used in the Sun AWT implementations.
Identifier | Meaning | Default |
---|---|---|
KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS | Normal forward keyboard traversal | TAB on KEY_PRESSED, CTRL-TAB on KEY_PRESSED |
KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS | Normal reverse keyboard traversal | SHIFT-TAB on KEY_PRESSED, CTRL-SHIFT-TAB on KEY_PRESSED |
KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS | Go up one focus traversal cycle | none |
Using the AWTKeyStroke API, client code can specify on which of two specific KeyEvents, KEY_PRESSED or KEY_RELEASED, the focus traversal operation will occur. Regardless of which KeyEvent is specified, however, all KeyEvents related to the focus traversal key, including the associated KEY_TYPED event, will be consumed, and will not be dispatched to any Component. It is a runtime error to specify a KEY_TYPED event as mapping to a focus traversal operation, or to map the same event to multiple default focus traversal operations.
If a value of null is specified for the Set, this Component inherits the Set from its parent. If all ancestors of this Component have null specified for the Set, then the current KeyboardFocusManager's default Set is used.
This is useful, for example, if running under full-screen mode and better performance is desired, or if page-flipping is used as the buffer strategy.
x
and y
parameters in the coordinate space of this component's parent.p
. Point
p
is given in the parent's coordinate space.getMaximumSize
will always
return this value. Setting the maximum size to null
restores the default behavior.getMinimumSize
will always
return this value. Setting the minimum size to null
restores the default behavior.getPreferredSize
will always
return this value. Setting the preferred size to null
restores the default behavior.d.width
and height d.height
.width
and height height
.b
.
If this component is not a lightweight component, the
AWT calls the update
method in response to
a call to repaint
. You can assume that
the background is not cleared.
The update
method of Component
calls this component's paint
method to redraw
this component. This method is commonly overridden by subclasses
which need to do additional work in response to a call to
repaint
.
Subclasses of Component that override this method should either
call super.update(g)
, or call paint(g)
directly from their update
method.
The origin of the graphics context, its
(0
, 0
) coordinate point, is the
top-left corner of this component. The clipping region of the
graphics context is the bounding rectangle of this component.
Note: For more information on the paint mechanisms utilitized by AWT and Swing, including information on how to write the most efficient painting code, see Painting in AWT and Swing.
Container
.
The current thread must own this object's monitor. The thread
releases ownership of this monitor and waits until another thread
notifies threads waiting on this object's monitor to wake up
either through a call to the notify
method or the
notifyAll
method. The thread then waits until it can
re-obtain ownership of the monitor and resumes execution.
As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:
synchronized (obj) { while (<condition does not hold>) obj.wait(); ... // Perform action appropriate to condition }This method should only be called by a thread that is the owner of this object's monitor. See the
notify
method for a
description of the ways in which a thread can become the owner of
a monitor.The current thread must own this object's monitor.
This method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. Thread T becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:
A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. In other words, waits should always occur in loops, like this one:
synchronized (obj) { while (<condition does not hold>) obj.wait(timeout); ... // Perform action appropriate to condition }(For more information on this topic, see Section 3.2.3 in Doug Lea's "Concurrent Programming in Java (Second Edition)" (Addison-Wesley, 2000), or Item 50 in Joshua Bloch's "Effective Java Programming Language Guide" (Addison-Wesley, 2001).
If the current thread is interrupted by another thread while it is waiting, then an InterruptedException is thrown. This exception is not thrown until the lock status of this object has been restored as described above.
Note that the wait method, as it places the current thread into the wait set for this object, unlocks only this object; any other objects on which the current thread may be synchronized remain locked while the thread waits.
This method should only be called by a thread that is the owner
of this object's monitor. See the notify
method for a
description of the ways in which a thread can become the owner of
a monitor.
This method is similar to the wait
method of one
argument, but it allows finer control over the amount of time to
wait for a notification before giving up. The amount of real time,
measured in nanoseconds, is given by:
1000000*timeout+nanos
In all other respects, this method does the same thing as the method of one argument. In particular, wait(0, 0) means the same thing as wait(0).
The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until either of the following two conditions has occurred:
notify
method
or the notifyAll
method.
timeout
milliseconds plus nanos
nanoseconds arguments, has
elapsed.
The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:
synchronized (obj) { while (<condition does not hold>) obj.wait(timeout, nanos); ... // Perform action appropriate to condition }This method should only be called by a thread that is the owner of this object's monitor. See the
notify
method for a
description of the ways in which a thread can become the owner of
a monitor.