Java Programming   Java Programming
 |Sofia Home | Content Gallery |
Home
Syllabus
Schedule
Lessons
Assignments
Resources

Lesson 10.3 Inherited Methods

Using Inherited Methods

The previous lesson taught you how to create a new subclass, the MyLabel class, which extends one of Java's built-in classes, java.awt.Label. You also learned that MyLabel could automatically use the methods defined in the Label class, all provided free via inheritance.

In this lesson, you'll take a closer look at the methods defined in the Component class. The Component class is the "grandparent" of your MyLabel class, and the parent of most of Java's AWT UI components. And, when it comes time to move to the Java Foundation Classes, you'll find that all of the Swing "J" classes are derived from java.awt.Component as well.

Let's take a look at MyLabel's inheritance.

The Component Class

The Component class is the superclass for most of Java's User-Interface [UI] objects. It contains over 100 methods and fields. You might be a little overwhelmed the first time you look through the methods in the Component class, but the time spent learning them is well worth it.

Methods defined in the Component class apply to all of the subclasses of Component. The time you spend learning the methods here is automatically multiplied by the number of different subclasses. If you learn to resize one Component, you've learned to resize them all. If you've learned to move one Component, you've learned to move them all.

The Method Index for the java.awt.Component class

Back to Top

Inherited Component Methods

Inheritance not only makes it easier to write new Components, it makes using Components simpler as well. Let's take a look at the most commonly used Component methods.

Component Colors

Every AWT Component has two color properties: a background color and a foreground property. The Component class provides methods to read both color properties like this:

Color bg = theButton.getBackground();
Color fg = theButton.getForeground();

It also provides methods to set both properties as well, like this:

theButton.setForeground(Color.red);
theButton.setBackground(Color.blue);

Because Java AWT components are based upon native peers, you might find that these methods do little or nothing on your system. To find out, you can write a simple applet like TestColor.java, shown here:

This program creates a pair of each of the following Components, with the foreground color set on the left and the background property set on the right.

The MyLabel objects in the top row are set to Color.red, the Label objects on the row following are set to Color.blue. Color.green is used for the Button objects on the third row. The List objects are Color.yellow, and the Scrollbars on the bottom are Color.pink.

On my Windows 98 machine, only setting the Scrollbar foreground property had no effect.

Back to Top

Component Size and Position

Before we start talking about explicitly setting Component sizes and positions, let me try to convince you that it is a really bad idea to do so. Here's why.

When you specify sizes or positions for Components in Java, you'll do so using pixels. If your user has a very high-resolution display, your 25 x 100 pixel Buttons may look tiny. If the user has a low-resolution display, the Buttons may look huge.

The only way out of this conundrum is to measure the Font size of the Button or Label and use that information when deciding how to size and position your Components. Of course, if you're willing to go to all that trouble, then using a layout manager, [which is what you should be doing], is less work after all.

The Rectangle Class

When talking about the size and position of an object, there are four relevant pieces of information:
  • x: The number of pixels from the left-hand side of the Component's container to the edge of the Component.

  • y: The number of pixels from the top of the Component's container to the top of the Component.

  • width: The number of pixels across your Component in a horizontal direction.

  • height: The number of pixels across your Component in a vertical dimension.
Java has a class that can hold all four of these pieces of information: the Rectangle class. A Rectangle object has four public fields: x, y, width, and height.

Like any other object, you can construct a Rectangle using a constructor like this:

Rectangle r =
new Rectangle(x, y, width, height);

Since the Rectangle's fields are public, you can change them like this:

r.width += 10;
r.height = 25;
Back to Top

getBounds() & setBounds()

To retrieve the current size and position of a Component, you send it the getBounds() method and it gives you back a Rectangle object. [Under Java 1.0, you had to use the bounds() method instead].

Likewise, you can change both the position and size of any Component by sending it the setBounds() method, which also takes a Rectangle as an argument. There is also a version of setBounds() that takes four ints if you'd rather not work with Rectangles. [Under Java 1.0, you had to use the reshape() method, rather than setBounds()]

When you explictly set the size of your Components, you need to first set the layout manager used by the Component's container to null. If you don't do this, then the layout manager will snatch your component back from you just when you have it as you like.

When you first use a null layout manager, you might think that something is wrong, but it's probably not. If there is no layout manager, then you must size and position each Component manually, or they will not show up at all.

Here's an amusing* applet, Breather.java, that uses a null layout manager and the Component methods getBounds(), setBounds(), and getSize(), along with the Rectangle method grow().

Back to Top

Size and Position

The Component class also has methods that allow you to set and retrieve the height and width of a Component, separately from its position on your applet. These methods are getSize() and setSize(). [In Java 1.0 you must use the size() and resize() methods.]

Rather than using a Rectangle object, the getSize() and setSize() methods work with a Dimension object. A Dimension, like a Rectangle, contains public fields that can be used directly. Instead of four fields, like the Rectangle class, the Dimension class has two, height and width.

Just as you can change the size of a Component without changing its position, you can also change the position, without changing the size. The methods you use for this are setLocation() and getLocation(). [Java 1.0 users must use move() and location() instead].

The setLocation() and getLocation() methods work with yet a third "helper" class, the Point class. Just as a Dimension contains only the height and width fields that a Rectangle object would have, a Point object contains only the x, y fields.

Something to Talk About

Here's a fun one. Download the "breathing button" applet Breather.java, and replace the Button object with a MyLabel object. What changes do you have to make to get it to work?

Please continue to the next section of this lesson.

 

Back to Top

 

Content Developed by Stephen Gilbert, Licensed under a Creative Commons License
Published by the Sofia Open Content Initiative
© 2004 Foothill-De Anza Community College District &The William and Flora Hewlett Foundation