Lecture 21, GUI

Reading

Slide handouts 21.

Selected parts of Java tutorial "Creating a GUI with JFC/Swing". 

Study Guide

This lecture is on Swing and GUI design in Java. The principles are simple, but the number of components are huge, and flexibility has been achieved at the cost of learning curve. Text fields and buttons are reasonable simple, but lists, tables, trees, and styled text are complex. There are three different aspects in practical GUI programming using swing:

Try to understand the examples from the lecture, and do the exercises. Actually, the exercises are very dependent on the examples from the lecture.

Exercises

The exercises here cannot be solved without extensive usage of the online Java API documentation. If you have managed to avoid using the API documentation before, now is your chance to use it. Open a browser at: http://java.sun.com/j2se/1.5.0/docs/api/ and go ahead. You will need the code examples from the lecture. They are available on the course page. One cannot learn Swing in one lesson, but the exercises will get you around some of the different aspects, and force you to browse through the API.  

Exercise 1 (mandatory)

  1. Get the ListExample from the lecture up and running. Add a ToolTip (JToolTip) to the field in which one writes part of the name to make the list shrink. We did not use tool tips at the lecture, so start up the browser and find out what a tool tip is, and try to use it.
  2. In the present user ListExample interface, the amount of space taken up by the the list to the left, and the fields to the right is fixed. A JSplitPane allow the user of the program to change the amount of space taken up by the two halves. Change the overall layout to use a JSplitPane, so you get the following layout, in which you can adjust the amount of horizontal space used by each part. 

Exercise 2 (mandatory)

  1. In the MVCExample.java from the lecture (the one with the two text fields) a thread was used directly to insert text into the document every 3 seconds. Instead of using a thread, use a javax.swing.Timer object. 
  2. Make use of the timer methods to ensure that no insertion of "HaHa" takes place for the first 10 seconds.
  3. Change usage of the timer so that only one "HaHa" is inserted. It should be inserted after 15 seconds.
  4. Add a JSlider to the MVC Example, which controls how many seconds should be between each insertion of "HaHa". It is acceptable if the new delay time is not used until after next insertion.

Exercise 3 (optional)

  1. In ListExample from the lecture, the present implementation of the list listens for changes to any part of the student. Change the update method in SelectingListModel so that the list is only updated when the name is changed. You only need to change the update method. 
  2. In ListExample, the list listens to each element in the list to find out if the name of the selected element changes. Change the code so that the list only listens on the selected element.