Widgets

Tom Kelliher, CS 116

Sept. 22, 2000

Administrivia

Announcements

Lab 2 write-ups due now. Postlab due Monday in hardcopy form. Follow our style guidelines.

Assignment

Read 3.3.

From Last Time

Lab 2 wrap-up, method design exercise.

Outline

  1. Quiz.

  2. Component abstract class.

  3. Textual widgets and an example Applet.

Coming Up

Active widgets.

Component Abstract Class

Abstract class:

A repository of methods and data (instance variables) that cannot be instantiated. It is a ``factoring out'' of methods and data from classes that have those methods and data in common.
Example: a class Car.

A portion of the Component hierarchy:

Component Operations

Some things you can do to a Component object:

  1. void setBackground(Color c)

    Wait we've seen this before. Where? What does this imply?

    Example:

    Label myLabel = new Label("Hi, I'm a label.");
    myLabel.setBackground(new Color(255, 0, 0));
    
    When we used setBackground() before, we didn't specify an object. The method was applied to what object, then?

  2. void paint(Graphics g)

    Empty. Each Component has one. We override the applet's paint() method. Widgets know how to ``paint'' themselves.

  3. void repaint()

    Request that a Component repaint itself. Will get done ``eventually.''

  4. Dimension getSize()

    Get the size, in pixels, of a Component.

    Examples:

    Dimension appletSize;
    
    appletSize = getsize();
    
    // We can now query the applet's size by accessing appletSize.width and
    // appletSize.height.
    
    Label myLabel = new Label("Hi, I'm a label.");
    Dimension labelSize;
    
    labelSize = myLabel.getSize();
    
    Again, not when you do and do not specify the object to which the method is applied.

Textual Widgets

Label Class

  1. Three Label constructors. Each has a unique signature.

  2. void setText(String label)

  3. String getText()

TextComponent Class

A factorization of TextField and TextArea, but not an abstract class.

  1. int getCaretPosition()

    Is this something Bugs Bunny would be interested in? What's a caret?

    0-based counting.

  2. String getSelectedText()

    What's a selection?

TextField Class

A one line text input object.

TextArea Class

A multi-line text input object.

Note: There are typos on pg. 82. The method declarations are correct; some of the example code uses the wrong method names. Check the Class References section of the chapter to double-check.

Example Applet

Demonstrates the use and actions of TextFields. Run the applet.

//======================================================================
//      Project:    CS 116, TextField example
//      Author:     Tom Kelliher
//      File:       TFExample.java
//      Purpose:    The Applet demonstrates the use of TextFields and
//                  Labels.  A simple, unchanging Label is created.
//                  Two Textfields are created.  The first displays the
//                  size of the Applet.  The second displays the
//                  previously selected text from the first.
//======================================================================

import java.applet.*;
import java.awt.*;

//======================================================================
//      Class:      TFExample
//      Purpose:    See the project purpose.
//======================================================================

public class TFExample extends Applet
{
   // Instance variables.
   Dimension appletSize;
   Label label = new Label("Size of the applet:");

   TextField display = new TextField(12);       // Display the size
                                                // of the applet.
   TextField selectedText = new TextField(12);  // Display selected
                                                // text from display
                                                // object.

   //===================================================================
   //   Method:     init
   //   Purpose:    Set Applet's background color.  Add and initialize
   //               widgets.
   //===================================================================

   public void init()
   {
      setBackground(Color.red);

      add(label);
      add(display);
      add(selectedText);

      label.setBackground(Color.green);
      label.setForeground(Color.red);

      display.setBackground(Color.blue);
      display.setForeground(Color.yellow);
      // Get Applet size and set text for display object.
      appletSize = getSize();
      display.setText(appletSize.width
                      + " by "
                      + appletSize.height);

      selectedText.setBackground(Color.yellow);
      selectedText.setForeground(Color.blue);
   }

   //===================================================================
   //   Method:     paint
   //   Purpose:    Update text in display and selectedText objects.
   //   Inputs:     g: A graphics object.
   //===================================================================

   public void paint(Graphics g)
   {
      String selection;   // Temporarily hold the selected text from
                          // the display object.  The selected text is
                          // copied to the selectedText object.

      selection = display.getSelectedText();

      // Update Applet size within display object.
      appletSize = getSize();
      display.setText(appletSize.width
                      + " by "
                      + appletSize.height);

      selectedText.setText(selection);
   }
}



Thomas P. Kelliher
Wed Sep 20 10:09:35 EDT 2000
Tom Kelliher