﻿ Java Tutorial

Main Page

Previous Next

Window-based Java Programs

Before we get into the programming specifics of window-based programs, we need to understand a little of how such programs are structured, and how they work. There are fundamental differences between the console programs that we have been producing up to now, and a window-based Java program. With a console program, you start the program, and the program code determines the sequences of events. Generally everything is predetermined. You enter data when required and the program will output data when it wants. At any given time, the specific program code to be executed next is generally known.

A window-based application, or an applet come to that, is quite different. The operation of the program is driven by what you do with the GUI. Selecting menu items or buttons using the mouse, or through the keyboard, causes particular actions within the program. At any given moment you have a whole range of possible interactions available to you, each of which will result in a different program action. Until you do something, the specific program code that is to be executed next is not known.

Event-driven Programs

Your actions when you're using the GUI for a window-based program or an applet – clicking a menu item or a button, moving the mouse and so on – are first identified by the operating system. For each action, the operating system determines which of the programs currently running on your computer should know about it, and passes the action on to that program. When you click a mouse button, it's the operating system that registers this and notes the position of the mouse cursor on the screen. It then decides which application controls the window where the cursor was when you pressed the button, and communicates the mouse button-press to that program. The signals that a program receives from the operating system as a result of your actions are called events.

A program is not obliged to respond to any particular event. If you just move the mouse, for instance, the program need not invoke any code to react to that. If it doesn't, the event is quietly disposed of. Each event that the program does recognize is associated with one or more methods, and when the event occurs – when you click a menu item, for example – the appropriate methods will be called automatically. A window-based program is called an event-driven program, because the sequence of events created as a result of your interaction with the GUI drives and determines what happens in the program.


Events are not limited to window-based applications – they are a quite general concept. Most programs that control or monitor things are event-driven. Any occurrence external to a program such as a switch closing, or a preset temperature being reached, can be registered as an event. In Java you can even create events within your program to signal some other part of the code that something noteworthy has happened. However, we're going to concentrate on the kinds of events that occur when you interact as a user with a program.

Previous Next
JavaScript Editor Java Tutorials Free JavaScript Editor