Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures


Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips


Submit News
Search the site here...
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

The Java Lesson 27: The Component class

JavaFAQ Home » Java Lessons by Jon Huhtala Go to all tips in Java Lessons by Jon Huhtala

Bookmark and Share
All Java Lessons contents page | Java Lesson 1 | Java Lesson 2 | Java Lesson 3 | Java Lesson 4 | Java Lesson 5 | Java Lesson 6 | Java Lesson 7 | Java Lesson 8 | Java Lesson 9 | Java Lesson 10 | Java Lesson 11 | Java Lesson 12 | Java Lesson 13 | Java Lesson 14 | Java Lesson 15 | Java Lesson 16 | Java Lesson 17 | Java Lesson 18 | Java Lesson 19 | Java Lesson 20 | Java Lesson 21 | Java Lesson 22 | Java Lesson 23 | Java Lesson 24 | Java Lesson 25 | Java Lesson 26 | Java Lesson 27 | Java Lesson 28 | Java Lesson 29 | Java Lesson 30 | Java Lesson 31 | Java Lesson 32 | Java Lesson 33 | Java Lesson 34 | Java Lesson 35 | Java Lesson 36 | Java Lesson 37 | Java Lesson 38 | Java Lesson 39 | Java Lesson 40 | Java Lesson 41 | Java Lesson 42 | Java Lesson 43 | Java Lesson 44 | Java Lesson 45 | Java Lesson 46

Java Lesson 27 by Jon Huhtala: The Component class

The Component class

  • Is an extension of the Object class


  • Is abstract so it can never be directly instantiated

  • Has been extensively subclassed to be the root of all non-menu related AWT components. Each subclass of Component inherits the same set of features and behaves in much the same way. Commonly used subclasses are:

  • Is the place to start when you need a way to control the appearance or behavior of an object on the screen. There are well over 100 methods to choose from (though many are deprecated).

Controlling position and size

  • Component objects always occupy a rectangle on the screen

  • Component position is always relative to the containing object (not to the overall screen) and is measured in pixels from the top-left corner. The top-left pixel has x and y coordinates of (0, 0) with x increasing to the right and y increasing downward.

  • Some commonly used methods related to component position and size are:




Gets the current size and location of the component


Gets the current height of the component


Gets the current location of the component


Gets the current size of the component


Gets the current width of the component


Gets the current x coordinate of the component's origin


Gets the current y coordinate of the component's origin


Moves and resizes the component


Moves the component


Resizes the component


  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

  • Each component can have its own background color, foreground (text) color, and font. If not specified, these settings will be the same as the component's container. If the container settings are not specified, the JVM will provide default settings.

  • Some commonly used methods related to component appearance are:




Gets the current background color of the component


Gets the current font of the component


Gets the current foreground color of the component


Sets the background color of the component


Sets the font of the component


Sets the foreground color of the component

Controlling user access and component visibility

  • Under program control, each component can be enabled, disabled, hidden, or visible

  • Some commonly used methods related to user access and component visibility are:




Determines whether the component is enabled


Determines whether the component is visible


Requests that this be the active component


Enables or disables the component


Shows or hides the component

Rendering (drawing) the component

  • Each component is automatically rendered (drawn) as needed by the JVM in response to window minimizing, maximizing, overlaying, etc.

  • Under program control, the following methods can be used to modify and render the component:




Gets a graphics context for modifying the component


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");

public App(String title) {
setLayout(new FlowLayout());
stop = new Button("STOP");
stop.setFont(new Font("SanSerif", Font.BOLD, 24));
go = new Button("GO");
go.setFont(new Font("SanSerif", Font.BOLD, 24));

public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(stop)) {
else {

public void windowClosing(WindowEvent e) {

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) {}


  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);

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search

Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy