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 26: Introduction to graphical programming and the java.awt packa

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

Introduction to graphical programming and the java.awt package

Graphical programming

  • Is used in Windows applications as well as web-based applets.

  • Provides a Graphical User Interface (GUI). It incorporates the mouse for user input and more visual display features, such as color images, for output.

  • Typically displays multiple controls (menus, buttons, scroll bars, etc.) upon which the user may act

  • Puts the user in charge. While text-based programs rigidly lead the user via prompts and simple menus, graphical programs provide a wide range of choices and let the user determine the order of actions.

  • Is event-driven and far more complex internally than text-based programming. The logic path is difficult to follow.

Event-driven programs

  • Define, arrange, and display components upon which the user may act. Typical components include buttons, text boxes, check boxes, radio buttons, scroll bars, and menu bars. The variety is nearly infinite.

  • Ask the JVM to be notified whenever the user acts upon specified components (clicks a button, selects a menu item, etc.). Such actions are called events.

  • Contain methods to handle each event. These perform the bulk of application or applet processing. What they do depends upon the application or applet and which control the user activated.

  • Are difficult to read. Logic does not flow from top to bottom like a procedural program. Rather, it jumps from one event handler to another in response to user actions.

The Abstract Windows Toolkit (AWT)

  • Is contained in the java.awt package. It consists of classes for creating user interfaces and for painting graphics and images.

  • Helps create components. In AWT terminology, a user interface object (such as a button) is a component. The Component class is the root of all AWT components.

  • Helps organize and arrange components. A container is a component that can contain components and other containers. A container can also have a layout manager that controls the visual placement of components within the container. The AWT package contains several layout manager classes and an interface for building your own layout manager.

  • Helps identify and respond to component events. Some components fire events when a user interacts with the components. The AWTEvent class and its subclasses are used to represent the events that AWT components can fire.

  • Is complex. Refer to the help facility of your development environment or the official Sun API specification for details.

Anatomy of a small windows program

The following is the source code of a small Java program to create and display a window having a single button:

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

public class App extends Frame implements WindowListener, ActionListener {

Button b;

public static void main(String[] args) {
App myWindow = new App("My first window");

public App(String title) {
setLayout(new FlowLayout());
b = new Button("Click me");

public void actionPerformed(ActionEvent e) {

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 import statements make it easy to access the necessary packaged AWT code

  2. By extending the Frame class, the App class inherits the features and look of a frame window

  3. The WindowListener interface is implemented in order to be notified of certain window events (such as iconifying, deiconifying, and closing the window)

  4. The ActionListener interface is implemented in order to be notified of certain action events to container components (such as a button being clicked)

  5. The Button reference, b, is an instance variable of the class

  6. The main() method is the first method executed. It instantiates the frame, sets its size, and makes it visible.

  7. The class constructor receives the frame's title as a parameter which it passes through to the superclass constructor. It then sets the layout manager of the frame to FlowLayout, registers a WindowListener so the frame will be notified of window events, instantiates the button and adds it to the frame, and registers an ActionListener for the button so the frame will be notified when the user clicks the button.

  8. The actionPerformed() method is required by the ActionListener interface. It is automatically called when the user clicks the button and receives an ActionEvent object as a parameter. In this sample, it handles the event by changing the label of the button.

  9. The windowClosing() method is required by the WindowListener interface. It is automatically called when the user closes the frame and receives a WindowEvent object as a parameter. In this sample, it disposes of frame resources and terminates the JVM.

  10. The remaining methods are required by the WindowListener interface. In this sample, they do nothing.

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. Assume a class named MyFrame properly extends Frame and has a constructor that accepts the frame's title as a parameter and sets the label component "Hello World!" within the frame's container. What will result from an attempt to compile and execute the following main() method?

public static void main(String[] args) {
MyFrame x = new MyFrame("Here goes");
  1. the label "Hello World!" will display in a frame that is wider than it is tall

  2. the label "Hello World!" will display in a frame that is taller than it is wide

  3. the program will compile but nothing will display

  4. a compile error will occur at line 2

  5. a compile error will occur at line 3

  1. Which of the following ends the processing of a Windows program written in Java?

  1. System.exit(0);

  2. dispose();

  3. reaching the end of the main() method

  4. the user clicking the close button for the window

  5. windowClosing(this);

  1. If zButton has been properly defined as the reference for a Button object, what will happen during program execution if the button is instantiated using only the following code? (choose four)

zButton = new Button("YES");
  1. the button will have the label "YES"

  2. the button component will be part of the frame's container

  3. the actionPerformed() method will be automatically called when the user clicks the button

  4. the button will remain in memory if the frame window is iconified

  5. the button's text could be changed by executing the statement: zButton.setLabel("NO");

  1. Which of the following methods is not required by the WindowListener interface?

  1. windowOpened(WindowEvent e)

  2. windowActivated(WindowEvent e)

  3. windowResized(WindowEvent e)

  4. windowDeiconified(WindowEvent e)

  5. windowClosed(WindowEvent e)

 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