Chapter 14

Graphical User Interfaces


Chapter Goals

Using Inheritance to Customize Frames

Example: Investment Viewer Program

Example: Investment Viewer Program

Of course, we still need a class with a main method:

Self Check

  1. How many Java source files are required by the investment viewer application when we use inheritance to define the frame class?
  2. Why does the InvestmentFrame constructor call setSize(FRAME_WIDTH, FRAME_HEIGHT), whereas the main method of the investment viewer class in Chapter 12 called frame.setSize(FRAME_WIDTH, FRAME_HEIGHT)?

Answers

  1. Three: InvestmentFrameViewer, InvestmentFrame, and BankAccount.
  2. The InvestmentFrame constructor adds the panel to itself.

Layout Management

Layout Management

Border Layout

Grid Layout

Grid Bag Layout

Self Check

  1. How do you add two buttons to the north area of a frame?
  2. How can you stack three buttons on top of each other?

Answers

  1. First add them to a panel, then add the panel to the north end of a frame.
  2. Place them inside a panel with a GridLayout that has three rows and one column.

Choices

Radio Buttons

Radio Buttons

Borders

Check Boxes

Combo Boxes

Combo Boxes

Radio Buttons, Check Boxes, and Combo Boxes

Classes of the Font Choice Program

File ChoiceFrameViewer.java

File ChoiceFrame.java

Self Check

  1. What is the advantage of a JComboBox over a set of radio buttons? What is the disadvantage?
  2. Why do all user interface components in the ChoiceFrame class share the same listener?
  3. Why was the combo box placed inside a panel? What would have happened if it had been added directly to the control panel?

Answers

  1. If you have many options, a set of radio buttons takes up a large area. A combo box can show many options without using up much space. But the user cannot see the options as easily.
  2. When any of the component settings is changed, the program simply queries all of them and updates the label.
  3. To keep it from growing too large. It would have grown to the same width and height as the two panels below it.

Advanced Topic: Layout Management

Advanced Topic: Layout Management

Menus

Menu Items

A Sample Program

File MenuFrameViewer.java

File MenuFrame.java

Self Check

  1. Why do JMenu objects not generate action events?
  2. Why is the name parameter in the createFaceItem method declared as final?

Answers

  1. When you open a menu, you have not yet made a selection. Only JMenuItem objects correspond to selections.
  2. The parameter variable is accessed in a method of an inner class.

Text Areas

Text Areas

File TextAreaViewer.java

Self Check

  1. What is the difference between a text field and a text area?
  2. Why did the TextAreaViewer program call textArea.setEditable(false)?
  3. How would you modify the TextAreaViewer program if you didn't want to use scroll bars?

Answers

  1. A text field holds a single line of text; a text area holds multiple lines.
  2. The text area is intended to display the program output. It does not collect user input.
  3. Don't construct a JScrollPane and add the textArea object directly to the frame.

Exploring the Swing Documentation

Example: A Color Mixer

Example: A Color Mixer

The Swing Demo Set

Example: A Color Mixer

How do I construct a JSlider?

How can I get notified when the user has moved a JSlider?

How can I tell to which value the user has set a JSlider?

The Components of the SliderFrame


Classes of the SliderFrameViewer Program


File SliderFrameViewer.java

SliderFrame.java

Self Check

  1. Suppose you want to allow users to pick a color from a color dialog box. Which class would you use? Look in the API documentation.
  2. Why does a slider emit change events and not action events?

Answers

  1. JColorChooser.
  2. Action events describe one-time changes, such as button clicks. Change events describe continuous changes.

Visual Programming