Principles of Swing

Goals

There are many reasonable overviews of Swing; we are going to use Head First Java Chapters 12 and 13 as our recommended reading because its freely accessible from the JHU subnet.

The main parts of Swing

Swing is very large, and you can drown in detail if you are not careful. Here is the 10,000 foot view.

Swing Windows: the JFrame class

Key points
  1. There is a single JFrame object associated with each Swing window
  2. frame.add(aComponent) will add a component (button, text field, compound component, etc) to the JFrame.
  3. Use frame.setVisible(true) to open the window; this method also spawns a thread which starts an event handling loop for the window.
  4. The window's event-handling thread terminates upon close of the window.
FrameOnly.java is a bare-bones JFrame that just opens a window.

Installing Components in a window

  1. Swing components are added to the JFrame's contentPane (a Container of window components) and they then appear in the window.
  2. You can specify the layout of the components in the window at the time they are installed (more later on that)
FrameAndLabel.java is a JFrame with a label added to its contentPane.

The Event Manager

Swing programming is event-driven

Here is an example that shows how it all works together to get the job done.
  1. "SAVE" button pressed -->
  2. event generated -->
  3. event queued -->
  4. ... -->
  5. event eventually gets to top of event queue -->
  6. your code gets called to react -->
  7. you save the file to disk -->
  8. your code returns, relinquishing control back to event thread -->
  9. next event processed or idle if the queue is empty

The different types of Events

Different event types are embodied in different classes

The handling of events

The details of event programming by example

LabelAndButton.java is a JFrame with a label and button which can be pressed to change the label.

Making Swing Components

Creating a custom Swing component

A custom component is a non-built-in widget.
CustomPanel.java has a MySimplePanel which does some simple circle drawing.
Here is how custom components work in Swing.
  1. Subclass your custom panel from JPanel, e.g. MySimplePanel extends JPanel
  2. Override the MySimplePanel paintComponent(Graphics g) method to do the drawing you desire
  3. Your first line of paintComponent method should be super.paintComponent(g); because in general the superclass could also be drawing on this surface (in this example it is not necessary). This is part of the Swing library contract.
  4. Argument g is passed to you -- its the graphical canvas you draw on.
  5. The Graphics class contains methods to draw on the canvas, eg. the g.fillOval() used here.
  6. (The window manager is smart to relativize g to the location of your component within the window, and to clip if you draw out of bounds.)

Component painting also uses the event manager

Custom components receiving mouse/keyboard input

If your component needs to respond to mouse/keyboard input, set up a mouse event listener.
SimpleSquaresUI.java has a custom component SquaresPanel that can respond to mouse clicks

Layouts: making it look pretty

Here are the most common layout managers you will be using.

There are various subtle issues on how much space these layout managers give different components and where they put them; run the above applications and see what happens, and watch again as you resize the windows.

The Composite Design Pattern in Swing

Complex layouts often need nested layouts
CompositePatternInSwing.java shows how JPanels (subclasses of JComponent) can contain JPanels which can contain JButton/JLabels.

The Built-in Swing Components

Component decorators

Common component decorator classes include the following.

The Model-View-Controller (MVC) design pattern

The MVC design pattern

Here is a summary illustration from the Oracle Swing docs which shows who does what:

Simple MVC Examples

Squares

We presented a simple mouse event example above, SimpleSquaresUI.java. The underlying data is a set of squares but it is so simple we do not make a separate model class. If this example was extended to additionally allow deletions/movement/etc of squares there starts to get enough functionality that we want to make a separate model and view.
SquaresUI.java and SquaresModel.java refactors SimpleSquaresUI into a separate model and view, and adds additional functionality.

BeatBox

The Head First Java book had a larger UI example of a beat box which munged together the underlying data for which sounds trigger when with the display of that data.
BeatBox.java Beat box example with no MVC - the model and view are all in one class.
BeatBoxUI.java and BeatBoxModel.java pulls out the underlying MIDI processing into its own model class, making it easier to understand and change these two components.

More Swing

While the above notes cover the key features, Swing is a massive beast.