Other Useful Classes


String

The String class is treated specially in Java. Unlike other classes, there is a Java syntax form for string literals: a sequence of characters enclosed in double quotes. Java also has an operator + that is used to concatenate strings end-to-end. When the Java compiler sees a string folowed by +, followed by an expression, the compiler can convert the expression to a string in a sensible way, no matter what its type is.

Since the compiler treats string specially, you rarely need the String constructor. The following methods are frequently useful.

The characters numbers in the substring() methods start at 0 for the first character in the string. In both versions, the returned string starts at the character indicated by the frm parameter. In the first version, everything from that point on is returned. In the second version, everything from that point up to, but not including the character indicated by the to parameter is returned.

Dimension

You will rarely need to do anything with the Dimension class other than specifying a Dimension object as a parameter in some method such as JComponent.setPreferredSize(Dimension). For this, you only need to use the following constructor.

Color

You will rarely need to do anything with the Color class other than specifying a Color object as a parameter in some method such as Component.setBackground(Color). The Color class defines the following constants which are predefined Color objects.

Font

You will rarely need to do anything with the Font class other than specifying a Font object as a parameter in some method such as Component.setFont(Font). For this, you only need to use the following constructor.

Some possible values for the family are "Helvetica", "Courier", or "Times-Roman". The Font constructor is forgiving of errors in the first parameter. If it cannot find a font family with the specified name, it will pick a family that it thinks is close.

The second parameter should be one of the predefined constants

Border

A Border object is used as a parameter in the JComponent.setBorder(Border) method. The best way to get a Border is with the BorderFactory class described in the next section.

BorderFactory

The BorderFactory class, as its name suggests, is a factory for producing Border objects. Here are a few of its methods.

The fact that these methods are static means that you will have to direct messages to the BorderFactory class as in the following exemple.

    panel.setBorder(BorderFactory.createTitledBorder("A Panel"));

TitledBorder

Occasionally, it is useful to change the title on a TitledBorder. The following method provides that capability.

To do this, you will need to have a variable of type TitledBorder. If that class name appears in a Java class definition, then the file should have the following import.

    import javax.swing.border.*;