Home

GUI Programming

GUI Programming

Programs with a graphical user interface (GUI) are quite different from programs that use terminal input and output. In a terminal-based program, the computation is drived by the program: It does its job, sometimes printing some output, sometimes requesting input from the user and pausing to wait for this input.

In a GUI program, the user is much more in control of the program: There are buttons to be pressed, the window can be moved, the menu activated, or an animation may be playing. This requires a different style of programming: Instead of following a fixed sequence of actions, a GUI program reacts to events. There are many different kinds of events: the user may click with the mouse, move the mouse button, press some key, close the window, move or resize the window, and so on.

At first sight, it's therefore a bit difficult to figure out what is happing in a GUI program. There are always two steps: First, we need to set up the windows and configure their contents. Then, the program doesn't do anything actively — it only acts by responding to events. These responses to events have been set up in the first phase.

We will use the Scala Swing library for building our user interfaces. Swing is actually a Java library, but using the Scala version is easier and more elegant.

All the code for this tutorial is available on github.