More AWT GUI Components

Java provides another set of components that are useful in developing GUI applications.

List (java.awt.List)

List components objects are useful for holding a large number of data items. They are quite similar to the Choice component, however they can allow for the multiple selection of items. The list automatically adds a scrollbar if it is required. The list constructor takes two arguments, the int number of items to be displayed and a boolean to allow/disallow multiple item selection.

 1 
 2 
 3   List aList = new List(3, true);
 4   aList.addItem("Some Item 1");
 5   aList.addItem("Some Item 2");
 6   aList.addItem("Some Item 3");
 7   aList.addItem("Some Item 4");
 8   //etc.
 9 
10 

Clicking on an item selects it and pressing again on the same item deselects it. The selection works the same way as Windows explorer file selection. i.e. if you hold the SHIFT key and select with the mouse you can select from one item in The getSelectedItem() (or getSelectedItems() for multiple selections) method can be used to retrieve the currently selected item.

Figure 9.13. A List Component

A List Component

TextArea (java.awt.TextArea)

Are used for creating areas of screen input where we want to provide large amounts of text data, either for displaying a text file, or implementing a text editor. TextArea components do not generate events. They can be set read-only or editable by using the setEditable() method.

Figure 9.14. A TextArea Component

A TextArea Component

The following are the constructors for a TextArea component


  TextArea()                        // empty string as text. 
  TextArea(int rows, int columns)   // number of rows and columns specified
  TextArea(String text)             // with the specified text. 
  TextArea(String text, int rows, int columns)  
          // with the specified text, and with the specified number of 
          // rows and columns. 
  TextArea(String text, int rows, int columns, int scrollbars)  
          // with the specified text, and with the rows, columns, and 
          // scroll bar visibility as specified either as SCROLLBARS_BOTH, 
          // SCROLLBARS_HORIZONTAL_ONLY, SCROLLBARS_NONE 
          // or SCROLLBARS_VERTICAL_ONLY 
          

The TextArea object can be created as follows:


  TextArea t = new TextArea("Test TextArea", 30, 5, 
                            TextArea.SCROLLBARS_VERTICAL_ONLY);

Scrollbar (java.awt.Scrollbar)

Scrollbar objects are used to develop sliders that choose values in a certain range of data. We can use a Scrollbar to control an input value, scroll an image, and numerous other applications. The constructor of a Scrollbar takes five parameters:

  • The orientation of the scrollbar, HORIZONTAL or VERTICAL.

  • The initial value of the slider.

  • The thickness of the slider drag component.

  • The minimum value of the scrollbar.

  • The maximum value of the scrollbar.

Use the getValue() method to read the current value of the Scrollbar object. Use the setValue() method to set the slider position in the scrollbar.

Figure 9.15. A Scrollbar Component

A Scrollbar Component

So an example use could be:


  Scrollbar sb = new Scrollbar(Scrollbar.HORIZONTAL, 50, 10, 0, 100);