The Component class

 

The Component class

Object  

\

Component 
Button
Canvas
Checkbox
Choice
Container
Label
List
Scrollbar
TextComponent

 

Controlling position and size

Method

Usage

getBounds()

Gets the current size and location of the component

getHeight()

Gets the current height of the component

getLocation()

Gets the current location of the component

getSize()

Gets the current size of the component

getWidth()

Gets the current width of the component

getX()

Gets the current x coordinate of the component's origin

getY()

Gets the current y coordinate of the component's origin

setBounds()

Moves and resizes the component

setLocation()

Moves the component

setSize()

Resizes the component

Notes:

  1. Many of these methods are overloaded to accept a range of parameter types and values.

  2. If a layout manager is controlling the container, these methods may be ineffective. Layout managers will be presented later.

 

Controlling appearance

Method

Usage

getBackground()

Gets the current background color of the component

getFont()

Gets the current font of the component

getForeground()

Gets the current foreground color of the component

setBackground()

Sets the background color of the component

setFont()

Sets the font of the component

setForeground()

Sets the foreground color of the component

 

Controlling user access and component visibility

Method

Usage

isEnabled()

Determines whether the component is enabled

isVisible()

Determines whether the component is visible

requestFocus()

Requests that this be the active component

setEnabled()

Enables or disables the component

setVisible()

Shows or hides the component

 

Rendering (drawing) the component

Method

Usage

getGraphics()

Gets a graphics context for modifying the component

repaint()

Repaints (renders) the component

Note: Drawing with a Graphics object will be presented later.

 

A sample program

The following is the source code of a small Java program to create and display a window having a two buttons of different colors:

import java.awt.*;
import java.awt.event.*;

public class App extends Frame implements WindowListener, ActionListener {

  Button stop;
  Button go;

  public static void main(String[] args) {
    App myWindow = new App("Stop and go");
    myWindow.setSize(200,150);
    myWindow.setBackground(Color.black);
    myWindow.setVisible(true);
  }

  public App(String title) {
    super(title);
    setLayout(new FlowLayout());
    addWindowListener(this);
    stop = new Button("STOP");
    stop.setBackground(Color.red);
    stop.setForeground(Color.white);
    stop.setFont(new Font("SanSerif", Font.BOLD, 24));
    stop.setEnabled(false);
    add(stop);
    stop.addActionListener(this);
    go = new Button("GO");
    go.setBackground(Color.green);
    go.setForeground(Color.white);
    go.setFont(new Font("SanSerif", Font.BOLD, 24));
    go.setEnabled(true);
    add(go);
    go.addActionListener(this);
  }

  public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(stop)) {
      stop.setEnabled(false);
      go.setEnabled(true);
    }
    else {
      go.setEnabled(false);
      stop.setEnabled(true);
    }
  }

  public void windowClosing(WindowEvent e) {
    dispose();
    System.exit(0);
  }

  public void windowOpened(WindowEvent e) {}
  public void windowActivated(WindowEvent e) {}
  public void windowIconified(WindowEvent e) {}
  public void windowDeiconified(WindowEvent e) {}
  public void windowDeactivated(WindowEvent e) {}
  public void windowClosed(WindowEvent e) {}
}

Notes:

  1. The Button references stop and go are instance variables of the class

  2. The main() method sets the frame's size, its background color, and makes it visible

  3. As part of its work, the class constructor instantiates each button, sets the button's background color, foreground color, font, accessibility, adds the button to the frame, and registers its ActionListener. The stop button is initially disabled and the go button is initially enabled. The Color and Font classes will be covered in a later lesson.

  4. The actionPerformed() method is automatically called when the user clicks either button and receives an ActionEvent object as a parameter. The getSource() method of the ActionEvent object returns a reference to the object that was acted upon. The referenced object is immediately compared to the stop button to determine if it was clicked. The logic of this sample simply flips which button is currently enabled.

 

Lab exercise for Ferris students

E-mail your answers to this assignment no later than the due date listed in the class schedule.

 

Review questions

  1. If the someComponent.getX() returns 0 and someComponent.getY() returns 0, which one of the following statements are true?

  1. the top-left corner of someComponent is in the top-left corner of the screen

  2. the top-left corner of someComponent is in the top-left corner of its container

  3. someComponent will not be visible

  4. someComponent has no size

  5. the bottom-left corner of someComponent is in the bottom-left corner of its container

  1. Assume that fiddle and twiddle reference properly instantiated components. Which one of the following statements would assure that component twiddle uses exactly the same font as component fiddle?

  1. twiddle.Font = fiddle.Font;

  2. twiddle.setFont(fiddle.Font);

  3. twiddle.Font = fiddle.getFont();

  4. twiddle.setFont(fiddle.getFont()); 

  5. fiddle.setFont(twiddle.getFont());

  1. A Label is constructed and given a foreground color of red. No background color is specified. It is then added to a Frame that has a foreground color of white and a background color of black. Which one statement is true of the Label?

  1. its text will appear with red letters on a black background

  2. its text will appear with red letters on a white background

  3. its text will appear with red letters on a background whose color is unpredictable

  4. both its text color and background color are unpredictable

  5. a run-time error will occur because no background color was specified

  1. Which one of the following statements will hide a component whose reference is showTime?

  1. showTime.isVisible(false);

  2. showTime.setEnabled(false);

  3. showTime.setVisible(false);

  4. showTime.setHidden(true);

  5. showTime.isHidden(true);