Java Reference
In-Depth Information
Note that the size of a component is managed automatically when it has a parent component. I explain how
this works later in this chapter. A component also has a minimum size and a maximum size. The size of the
component lies within the range from the minimum to the maximum, and if the space available to it is less
than the minimum size, the component is not displayed. You can set the preferred size for a component as
well as the minimum and maximum size. This provides a way for you to influence the size of a component
when it is displayed.
The methods defined in the Component class that retrieve the size and position are the following:
Rectangle getBounds() : Returns the position and size of the object as an object of type Rect-
angle .
Rectangle getBounds(Rectangle rect) : Stores the position and size in the Rectangle object
that you pass as the argument and returns a reference to rect. This version of the method enables
you to reuse an existing Rectangle object to store the bounds. If rect is null, a new Rectangle
object is created by the method.
Dimension getSize() : Returns the current size of the Component object as a Dimension object.
Dimension getSize(Dimension dim) : Stores the current size in dim and returns a reference to
dim. This enables you to reuse an existing Dimension object.
Point getLocation() : Returns the position of the Component object as an object of type Point .
Point getLocation(Point p) : Stores the coordinates of the current position of the component
in the argument, p , and returns a reference to p . This enables you to reuse an existing Point object
to store the position.
You can also change the size and/or position of a component by using the following methods:
void setBounds(int x, int y,int width, int height) :Sets the position of the Component
object to the coordinates ( x , y ) and the width and height of the object to the values defined by the
third and fourth arguments.
void setBounds(Rectangle rect) : Sets the position and size of the Component object to be
that of the Rectangle argument rect .
void setSize(Dimension d) : Sets the width and height of the Component object to the values
stored in the members of the object d .
setLocation(int x, int y) : Sets the position of the component to the point defined by ( x , y ).
setLocation(Point p) : Sets the position of the component to the point p .
You can also set the parameters that determine the range of variation in size that is possible for a com-
ponent with the following methods:
void setMinimumSize(Dimension d) : Sets the minimum size of the Component object to the
dimensions specified by the argument d . A null argument restores the default minimum size for
the component.
void setMaximumSize(Dimension d) : Sets the maximum size of the Component object to the
dimensions specified by the argument d . A null argument restores the default maximum size for
the component.
void setPreferredSize(Dimension d) : Sets the preferred size of the Component object to the
dimensions specified by the argument d . A null argument restores the default preferred size for the
component.
Another important method defined in the Component class is getToolkit() . This returns an object of
type Toolkit that contains information about the environment in which your application is running, includ-
Search WWH ::




Custom Search