12 Mar 2012 [ 201 week9 ]

We'll start looking at interfaces in Java, and using them especially in the context of a graphical user interface we'll build using the built-in Swing library. We'll also start learning about how event-driven programming works in Java.

A few important interfaces we'll cover:

GUI example

The examples we worked on are in the swing-examples project, which will eventually display a sorted list of all names and ages typed in. Here's our current progress:

package org.ilzd.swingexample;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 * An example we'll use to build a list of names and ages.
 * 
 */
public class SwingExample implements ActionListener {
    private JTextField age;
    private JTextField name;
    private PersonModel people;
    
    public void actionPerformed(ActionEvent ae) {
        System.out.println(name.getText() + ", " + age.getText());
        int ageInt = Integer.parseInt(age.getText());
        people.add(name.getText(),ageInt);
    }

    public SwingExample() {
        people = new PersonModel();
        
        // Create main window
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Set main layout
        frame.setLayout(new BorderLayout());

        // Create input panel
        JPanel input = new JPanel();
        input.setLayout(new GridLayout(1, 5));
        JLabel nameLabel = new JLabel("Name:");
        name = new JTextField();
        JLabel ageLabel = new JLabel("Age:");
        age = new JTextField();
        JButton button = new JButton("Add");
        input.add(nameLabel);
        input.add(name);
        input.add(ageLabel);
        input.add(age);
        input.add(button);

        button.addActionListener(this);

        // Add input panel to main window
        frame.add(input, BorderLayout.PAGE_START);

        // Set up scrollable list
        JList list = new JList();
        JScrollPane scroller = new JScrollPane();
        scroller.getViewport().setView(list);
        list.setModel(people);

        // Add to main window
        frame.add(scroller, BorderLayout.CENTER);

        // Set up the full layout and show the window
        frame.pack();
        frame.setVisible(true);
    }
    
    public static void main(String[] args) {
        new SwingExample();
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ilzd.swingexample;

import java.util.ArrayList;
import java.util.Collections;

public class Person implements Comparable<Person> {
    public String name;
    public int age;
    
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
        
    public String toString() {
        return name + ", " + age;
    }

    public int compareTo(Person t) {
        if (age < t.age) {
            return -1;
        } else if (age > t.age) {
            return 1;
        } else {
            return name.compareTo(t.name);
        }
    }
    
    public static void main(String[] args) {
        ArrayList<Person> people = new ArrayList<Person>();
        people.add(new Person("Bob", 20));
        people.add(new Person("Fred", 18));
        people.add(new Person("Alice", 20));
        people.add(new Person("Ziggy", 85));
        
        
        
        Collections.sort(people);
        for (Person p : people)
            System.out.println(p);
    }

}

Readings

Read the "Object-Oriented Design" chapter in the book.

Also take a look at these detailed tutorials on Swing: