Java Reference
In-Depth Information
• The
font
used by the object when text is displayed.
• 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 dis-
abled component can still originate events, which I will discuss in Chapter 18.
• 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, the 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 then needs 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 indir-
ectly in some way because all of the data members that store its characteristics are
private
. For example,
you can change the name of a
Component
object
myWindow
with the statement:
myWindow.setName("The Name");
To retrieve the name of a component you 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 component is visible, en-
abled, and valid, respectively. Calling
validate()
for a components makes it valid. You can set a com-
ponent as visible or enabled by passing the value
true
to the methods
setVisible()
and
setEnabled()
,
respectively.
A common misconception with Swing components is that calling
setEnabled(false)
inhibits events
such as mouse clicks from a component. This is not the case. All it does is to set the internal enabled status
for the component to
false
, causing 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
isEn-
abled()
method returns
false
.
Let's see how you 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
java.awt.Point
. A
Point
ob-
ject 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
java.awt.Dimension
. The
Dimension
class 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
java.awt.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 are of type
int
.
Components have a “preferred" size defined by a
java.awt.Dimension
object encapsulating values for
the width and the height. The preferred size varies depending on the particular object. For example, the pre-
ferred size of a
JButton
object that defines a button is the size that accommodates the label for the button.