Chapter 3. Swing Component Basics
3.3 The JComponent Class
3.3.2 JComponent Properties
3.3.2.4 Position, Size, and Alignment
You can set and retrieve a Swing component's current position and size on the screen through the
bounds property, or more precisely, through the location and size properties of JComponent. The location property is defined as a Point in the parent's coordinate space where the upper-left
corner of the component's bounding box resides. The size property is a Dimension that specifies
the current width and height of the component. The bounds property is a Rectangle object that
gives the same information: it bundles both the location and the size properties. Figure 3.6 shows
how Swing measures the size and location of a component.
Unlike the AWT Component class, the getBounds() accessor in JComponent can take a pre-
instantiated Rectangle object, as shown below: Rectangle myRect = new Rectangle(); myRect = component.getBounds(myRect);
If a Rectangle is supplied, the getBounds() method alters each of the fields in the passed-in Rectangle to reflect the component's current size and position, returning a copy of it. If the
reference passed in is a null, the method instantiates a new Rectangle object, sets its values, and
returns it. You can use the former approach to conserve memory if there are several calls to
getBounds().
The setBounds() method resets the component's size and position. This method also takes a Rectangle object. If the new settings are a change from the previous settings, the component is
moved, typically resized, and invalidated. If the component has a parent, it is invalidated as well. Be warned that various layout managers may override any changes you attempt to make to the bounds
property. Invalidating a component with a call to setBounds() may force the layout manager to
recompute and reset the bounds of the component in relation to the other components—resolving it to the same size as before.
Here is a short example that shows how to retrieve the current position and size of any Swing component:
JFrame frame = new JFrame("Test Frame"); frame.setBounds(20,20,200,200);
frame.setVisible(true);
Rectangle r = new Rectangle(); r = frame.getBounds(r);
System.out.println("X = " + r.x()); System.out.println("Y = " + r.y()); System.out.println("Width = " + r.width()); System.out.println("Height = " + r.height());
There is a shorthand approach for retrieving each of the bounds properties. JComponent contains
four methods that directly access them: getX() , getY(), getWidth(), and getHeight(). You can
use these accessors directly instead of instantiating a Rectangle object on the heap with a call to getBounds(). Consequently, you can replace the last six lines with the following four:
System.out.println("X = " + frame.getX()); System.out.println("Y = " + frame.getY()); System.out.println("Width = " + frame.getWidth()); System.out.println("Height = " + frame.getHeight());
In addition, if it is just the size or location you are concerned with, you can use the getSize() and getLocation() accessors to set or retrieve the size or location. The size is specified as a
Dimension, while the location is given as a Point. Like getBounds(), the getLocation()
accessor also allows the programmer to pass in a pre-instantiated Point object. If one is passed in,
the method alters the coordinates of the Point instead of instantiating a new object. Point myPoint = new Point();
myPoint = component.getLocation(myPoint);
You can still use the setSize() and setLocation() methods of java.awt. Component if you
prefer to code with those as well. Again, note that when resetting the size of the component, the layout manager may override the new value and reset it back to its previous value, thus ignoring your new size values.
The three well-known AWT sizing properties, minimumSize , preferredSize , and maximumSize ,
are accessible through JComponent. minimumSize indicates the smallest size that the component is
allowed to be when it exists in a container. preferredSize contains the size at which the
container's layout manager should strive to draw the component. maximumSize indicates the largest
size the component should be when displayed in a container. If none of these properties are set by the user, they are always calculated by the component's UI delegate or directly by the layout manager of the container, in that order. An important new feature of JComponent is the addition of
the methods setMinimumSize(), setPreferredSize, and setMaximumSize(), allowing you to
change these properties without subclassing.
Finally, JComponent contains two read/write properties that help interested layout managers align
the component in a container: alignmentX and alignmentY. Both of these properties contain
floating-point values between 0.0 and 1.0; the numbers determine the position of the component relative to any siblings. A number closer to indicates that the component should be positioned closer to the left or top side, respectively. A perfect 0.5 indicates that the component should be placed at the center, while a number nearing 1 indicates that the component should be positioned closer to the right or bottom. Currently, the only layout managers that use these properties are the BoxLayout
and OverlayLayout managers; all AWT 1.1 layout managers ignore these properties and position
their children by other means. We discuss these managers further in Chapter 11.