Lesson 9

Graphical User Interface - GUI

A GUI is a graphical user interface to a computer. You are looking at the GUI of your particular Web browser. The term came into existence because the first interactive user interfaces to computers were not graphical; they were text-and-keyboard oriented and usually consisted of commands you had to remember and computer responses that were infamously brief. The command interface of the DOS operating system is an example of the typical user-computer interface before GUIs arrived. Green screens to mainframes are another example. An intermediate step in user interfaces between the command line interface and the GUI was the non-graphical menu-based interface, which let you interact by using a mouse rather than by having to type in keyboard commands.

Java plays an important part in the development of GUIs. Currently major operating systems provide a graphical user interface. Applications typically use the elements of the GUI that come with the operating system and add their own graphical user interface elements and ideas. This is an example of reusable components. A GUI sometimes uses one or more metaphors for objects familiar in real life, such as the desktop or the view through a window. Elements of a GUI include such things as: windows, pull-down menus, buttons, scroll bars, iconic images, and the mouse. With the increasing use of multimedia as part of the GUI, sound, voice, motion video, and virtual reality interfaces seem likely to become part of the GUI for many applications. A system's graphical user interface along with its input devices is sometimes referred to as its "look-and-feel." Many of the features of Java lend itself to GUI implementation.

When creating an application, many object-oriented tools exist that facilitate writing a graphical user interface. Each GUI element is defined as a class from which you can create object instances for your application. You can code or modify prepackaged methods that an object will use to respond to user interaction.

Graphical User Interface with Java

The simplest of AWT (Java’s Abstract Windowing Toolkit) component is the basic UI component. You can create and add these to your applet without needing to know anything about creating container or panels. An applet is already an AWT container. Basic UI components: labels, buttons, checkboxes, radio buttons, choice menus, text fields, and scrolling lists. The procedure for creating the component is the same: you can create the component, and then add it to the panel that holds it, at which point it is displayed on the screen. To add a component to a panel, use add() method:

   public void init( ) {
     Button b = new Button("OK");
     add(b);
   }

Where the component appears in the panel depends on the layout that panel is defined to have. Each of these components has an action associated with it, something that component does when it is activated. Actions generally trigger events or other activities in your applet. Some more examples of actions include exceptions or server based activities.

Labels are text strings that you can use to label other UI components. The advantages that a label has over an ordinary text string is that it follows the layout of the given panel, and it does not need repainting every time the panel is redrawn.

Buttons are GUI components that trigger some action in your interface when they are pressed.

Checkboxes can be selected or deselected to provide options. Checkboxes are user interface components that have two states: on and off (or checked and unchecked, selected and unselected, true and false). Unlike buttons, checkboxes usually do not trigger direct actions in a GUI but, instead, are used to indicate optional features of some other action.

Radio Buttons are a variation on the checkbox. They have the same appearance as checkboxes, but only one in a series can be selected at a time.

Choice Menus are more complex GUI components than labels, buttons, or checkboxes. Choice menus are popup or pulldown menus that enable you to select an item from that menu. The menu then displays that choice on the screen.

Text Fields allow you to enter any values. Text fields enable your reader to enter text.

Scrolling Lists are functionally similar to a choice menu in that it lets you pick several options from a list.


Below is a program that demonstrates user interaction. Compile the code, write a separate html file that holds the applet, and run the code in the Java appletviewer. Then use the mouse to click (single or double) on the appletviewer, and see the results in the DOS window.


Assignment 9

- Write a simple GUI, one which includes most of the GUI components listed above.

  GO BACK
Return to the Top