Principles of the Swing Framework

What this lecture covers

We are going to use some of the examples from the Core Java books in these notes. The examples are available for download (local unzipped copy).

Swing Overview

Here are the main components we will cover

Swing Windows: the JFrame class

Here are some key points about Swing windows
  1. JFrame is the abstract window class
  2. Subclass your concrete MyDeliciousFrame extends JFrame
  3. Each window you see on the screen has exactly one JFrame object associated with it.
  4. Use frame.show() to open the window.
  5. show() spawns a thread which
    • opens the window;
    • starts an event handling loop for the window.
  6. All programming relative to the window is then in event-driven style. (more later on events)
  7. Window event-handling threads terminate upon close of window.
The Horstmann SimpleFrameTest.java shows a simple example of opening a simple window.

The Component Structure of Windows

(You don't really need to know this, it is a detour into Swing design choices)

Installing Components in a window

  1. Components are added to the JFrame's contentPane Container.
  2. You can specify the 2-D layout of the components in the window at the time they are installed
  3. Note a Container is also a Component (via subclass relationship) so the component structure of a window can be nested for reuse of groups of widgets.
    (This is a clever design that is useful in many other contexts: its a design pattern, the Composite pattern.)

Creating a custom Swing component

See the NotHelloWorld.java example.
  1. Subclass your custom panel from JPanel, e.g. NotHelloWorldPanel extends JPanel in this example
  2. Override NotHelloWorldPanel method paintComponent(Graphics g) to do the drawing you desire
  3. If your component needs to respond to mouse/keyboard input, set up an event listener (see below).
  4. Argument g is passed to you -- its the graphical canvas you draw on.
  5. The window manager is smart to relativize g to the location of your component within the window.
  6. The actual drawing/painting is directly performed by methods of the Graphics g. In this particular example it is simply
    g.drawString("Not a Hello, World program", 75, 100)
  7. Your first line of paintComponent method should be super.paintComponent(g); to make sure the enclosing context is also painted. This is part of the Swing library contract.

The event-driven nature of component painting

Time to look at events in more detail.

The Event Manager

See the Sun Tutorial for more information on this topic. Note the event manager is in package java.awt.event.

Swing programming is event-driven (a.k.a. reactive) programming

The different sorts of Events

Different events are embodied in different classes Note the idea of objects which hold events is an advanced programming pattern (reification of an action as an object), and it may seem a bit odd at first.

The handling of events

The event programming model in more detail

Consider the simple ButtonTest.java.

How a low-level mouse click is handled

Here is another example of an event and listener, in this case a custom component listening to mouse clicks
  1. User clicks mouse on custom JPanel.
  2. This gets packaged as a MouseEvent object by the system and put on the event manager queue.
  3. When that event comes to top of queue, event manager delegates that mouse event to the button component by looking at what component sits on the x,y position of the click.
  4. That component has a listener, a MouseListener, attached to it
  5. Method mousePressed() on that MouseListener is invoked
  6. MouseListener is in fact just an interface: the user wrote their own class implementing MouseListener that has method MousePressed().
See the Mousetest.java class MouseHandler for such a listener.

This example is also more complex in that mouse motions (without clicking) are also detected by a distinct MouseMotionListener.

The Built-in Swing Components

We briefly cover some principles of the built-in Swing components.

The Model-View-Controller (MVC) design pattern

The built-in Swing components are all based on MVC

The MVC design pattern (not a "Gang of Four" pattern)

Use of MVC in built-in Swing components

Let us consider the case of a JButton here.

The Template for a Built-In Swing Component

Now we will go through details of the built-in Swing component types. Here is the approximate pattern.

Swing Text Components

We will look at these in a bit more detail as a case study of the built-in Swing compnents; the rest are similar. See TextTest.java example for the details.

Customizing the model

Particular Components

Here is a very brief listing of the other built-in components and examples of their use.

TextArea

Scrollbars: an instance of the Decorator pattern: add a feature to a component and produce something thats still a component.

Labels

Labels are simple string labels in windows; see JLabel.

Check Boxes

JCheckBox is the class.

Buttons

JButton

Radio Buttons

JRadioButton

Borders

Border class.

Other Built-In Components

Dialogs

Dialogs are transient windows. For simple built-in Swing dialogs, use JoptionPane.

Making your own dialog