Java Foundation Classes

JFC is a collection of packages that provide GUI and graphics components:

java.awt = peer-based controls & event handling
javax.swing = peerless controls
accessibility API
2D Graphics API
drag-and-drop API
etc.

These notes will focus on building GUIs using swing controls and awt event handling.

Graphics

Before beginning with Java GUIs, it's a good idea to review graphics programming in Java.

Building a Graphical User Interface with Swing and Model-View-Controller Pattern

Building a Swing GUI

Layout

Positioning components in a panel (i.e., layout) is a weak point in Java. There are three techniques: use a drag-n-drop GUI editor (line the one in NetBeans), use the complex GridBagLayout Manager, or use a technique I call nested panels.

Every panel has an associated layout manager that determines where child components are placed. This is essentially the Strategy design pattern. Here's a quick review of layout in JFC. An example explains the nested panels technique.

Event Handling

GUI controls such as buttons and menu items fire events when they are selected by the user. Registered listeners automatically handle these events. Recall that this is just an instance of the Publisher-Subscriber Pattern. Take a minute to review Java's event delegation model.

Examples

Two simple examples

Painting: Handling mouse and keyboard events and using dialog boxes

Menus

Tables

Multi-View Applications

Editors

Graphs