<Previous    Back to Start  Contents   Next>


1.2 Exam Only: The java.awt package - Components and Facilities


Write code to demonstrate the use of the following methods of the java.awt.Component class: setVisible(boolean), setEnabled(boolean), getSize(), setForeground() and setBackground().

The names of these methods are self-explanatory. Note that after you create a frame, there are not visible by default, you must use setVisible(true) before it can be seen.

getSize() returns a dimension object, which contains two integer member variables, height and width.

Obviously setForeground() and setBackground() take a color argument.


Construct a java.awt.TextArea or java.awt.List that prefers to display a specified number of columns.

As there is no mechanism to set the number of columns a list has, I would assume that the above objective contains a typo, and should refer to rows, not columns.

The constructors for a List are as follows:

List(int rows)
Creates a new scrolling list initialized with the specified number of visible lines. By default, multiple selections are not allowed.
List(int rows, boolean multipleMode)
Creates a new scrolling list initialized to display the specified number of rows. If the value of multipleMode is true, then the user can select multiple items from the list. If it is false, only one item at a time can be selected.

The constructors for a TextArea are under the next objective.


Construct a java.awt.TextArea or java.awt.TextField that prefers to display a specified number of columns.

A TextArea object is a multi-line region that displays text. The following constructors allow you to specify the preferred number of rows and columns:

TextArea(int rows, int columns)
Constructs a new empty TextArea with the specified number of rows and columns.
TextArea(String text, int rows, int columns)
Constructs a new text area with the specified text, and with the specified number of rows and columns. This text area is created with both vertical and horizontal scroll bars.
Note: It is the number of rows, then columns. This is counter-intuitive, as usually in programming you specify a horizontal dimension, then a vertical one, so this exception can catch you out.

A TextField has a single line of text:

TextField(int columns)
Constructs a new empty TextField with the specified number of columns.
TextField(String text, int columns)
Constructs a new text field initialized with the specified text to be displayed, and wide enough to hold the specified number of characters.

State the significance of a "column" where one of the text components is using a proportional (variable) pitch font or a fixed pitch font.

The width of a "column" is equal to the width of a character, in the particular font being used. In the case of fixed pitch fonts, where all the characters are the same size, this is straightforward. For proportional fonts, the width of a column is equal to the average of the width of all the characters in the font. This means, for example, if you had text component with a proportional font and a width of 10 columns, if you use a narrow letter such as 'i', more than 10 'i's could be displayed.


<Previous    Back to Start  Contents   Next>

©1999, 2000, 2002 Dylan Walsh.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with the Invariant Sections being the disclaimer, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".