Java Reference
In-Depth Information
The
cursor
for the object - this defines the appearance of the cursor when it is over the object.
❑
Whether the object is
enabled
or not - when a component is enabled its enabled state is
true
and it has a normal appearance. When a component is disabled it is grayed out. Note that a
disabled component can still originate events.
❑
Whether the object is
visible
on the screen or not - if an object is not marked as visible it is
not drawn on the screen.
❑
Whether the object is
valid
or not - if an object is not valid, layout of the entities that make up
the object has not been determined. This is the case before an object is made visible. You can
make a
Container
object invalid by changing its contents. It will then need to be validated
before it is displayed correctly.
❑
You can only modify the characteristics of a
Component
object by calling its methods or affecting it
indirectly in some way since none of the data members that store its characteristics are directly
accessible - they are all
private
. For example, you can change the name of a
Component
object
myWindow
with the statement:
myWindow.setName("The Name");
If you subsequently want to retrieve the name of an object, you can use the
getName()
method which
returns the name as a
String
object. For example:
String theName = myWindow.getName();
The
isVisible()
,
isEnabled()
, and
isValid()
methods return
true
if the object is visible,
enabled, and valid respectively. You can set an object as visible or enabled by passing a value
true
as
an argument to the methods
setVisible()
and
setEnabled()
respectively.
A common misconception with Swing components is that calling
setEnabled(false)
will inhibit
events such as mouse clicks from the component. This is not the case. All it does is to set the internal
enabled status to
false
and cause the component to be grayed out. To prevent events from a disabled
component having an effect, you must call
isEnabled()
for the component in your event handling
code to determine whether the component is enabled or not. You can then choose to do nothing when
the
isEnabled()
method returns
false
.
Let's see how we can change the size and position of a
Component
object.
The Size and Position of a Component
Position is defined by
x
and
y
coordinates of type
int
, or by an object of type
Point
. A
Point
object
has two public data members,
x
and
y
, corresponding to the
x
and
y
coordinate values. Size is defined
by
width
and
height
, also values of type
int
, or by an object of type
Dimension
. The class
Dimension
has two public members of type
int
, namely
width
and
height
. The size and position
of a component are often specified together by an object of type
Rectangle
. A
Rectangle
object has
public data members,
x
and
y
, defining the top-left corner of the rectangle, with
width
and
height
members defining its size. All these data members are of type
int
.