CS 1410-20 Lab 10

In this lab, we will explore using the Java Swing GUI toolkit, which is offers a highly stateful set of classes and methods.

A Frame

Create a new project and a new class called PhoneBook. Use the following code, and run it:

  import javax.swing.*;
  
  public class PhoneBook {
      public static void main(String[] args) {
          JFrame frame = new JFrame("White Pages");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
          JLabel label = new JLabel("Phone number...");
          frame.getContentPane().add(label);
  
          frame.pack();
          frame.setVisible(true);
      }
  }

This program does not use the tester library. Instead, it defines a starting point in the normal Java way: by defining a static method (i.e., a function) called main.

How Many Frames?

What happens if you add

  JFrame frame2 = frame;

after the declaration of frame and change frame.getContentPane().add(label) to frame2.getContentPane().add(label)?

What happens if, instead of JFrame frame2 = frame;, you add the declaration

  JFrame frame2 = new JFrame("Other White Pages");

?

After you experiment, get rid of frame.

Multiple Labels

To add multiple labels to a frame, you need to create a Panel and add that to the frame instead. Then, you can add multiple labels to the Panel.

Try this program:

  import javax.swing.*;
  
  public class PhoneBook {
      public static void main(String[] args) {
          JFrame frame = new JFrame("White Pages");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
          JPanel panel = new JPanel();
          frame.getContentPane().add(panel);
  
          JLabel nameLabel = new JLabel("Name...");
          panel.add(nameLabel);
  
          JLabel numberLabel = new JLabel("Phone number...");
          panel.add(numberLabel);
  
          frame.pack();
          frame.setVisible(true);
      }
  }

Multiple Lines

By default, Panel arranges the things that you add to it left-to-right (until it runs out of space). To have a panel arrange items in a different way, you have to set its layout.

Add

  import java.awt.*;

to the beginning of your program, and then set the panel’s layout before packing the frame:

  panel.setLayout(new GridLayout(2, 1, 0, 0));

The 2 means two lines, and 1 means one column per line. The 0s indicate extra space to put between the rows and columns.

Buttons

Create a button by allocating an instance of the JButton class:

  JButton button = new JButton("Lookup");

Add it to your panel on its own line, below the two labels.

Button Clicks

To handle a button click, you add a listener to the button. A listener is an instance of a class that implements the ActionListener interface as exported by java.awt.event.*.

Here’s a listener class that sets a label string in response to a button click:

  class Lookup implements ActionListener {
      public void actionPerformed(ActionEvent e) {
          numberLabel.setText("555-1212");
      }
  }

You can put this class inside main so it can see numberLabel, but you have to add the keyword final before JLabel numberLabel. The keyword final simply means that you promise not to use numberLabel = ....

Connect Lookup to button like this:

  button.addActionListener(new Lookup());

Text Fields

Use new JTextField() to create a text field, and substitute it in place of nameLabel.

Change Lookup so that it uses the name in the name field to set numberLabel to “no number for <name>.”


Last update: Thursday, November 18th, 2010
mflatt@cs.utah.edu