Skip to main content
Logo image

Java, Java, Java: Object-Oriented Problem Solving, 2024E

Section 13.10 Chapter Summary

Subsection 13.10.1 Technical Terms

adapter class lightweight component
callback design listener
content pane model
containment hierarchy model-view-controller (MVC)
controller peer model
event model pluggable look and feel
inner class view
layout manager widget hierarchy

Subsection 13.10.2 Important Points

  • Java provides two sets of Graphical User Interface (GUI) components, the Abstract Windowing Toolkit (AWT), which was part of Java 1.0 and the Swing component set, the GUI part of the Java Foundation Classes (JFC), introduced in JDK 1.1.
  • Unlike their AWT counterparts, Swing components are written entirely in Java. This allows programs written in Swing to have a platform-independent look and feel. There are three built-in look-and-feel packages in Swing: a Windows style, a Unix-like Motif style, and a purely Java Metal style.
  • Swing components are based on the model-view-controller (MVC) architecture, in which the component is divided into three separate objects: how it looks (view), what state it’s in (model), and what it does (controller). The view and controller parts are sometimes combined into a single user interface class, which can be changed to create a customized look and feel.
  • AWT components are based on the peer model, in which every AWT component has a peer in the native windowing system. This model is less efficient and more platform dependent than the MVC model.
  • Java’s event model is based on event listeners. When a GUI component is created, it is registered with an appropriate event listener, which takes responsibility for handling the component’s events.
  • A user interface combines four functions: guidance/information for the user, input, output, and control.
  • Components in a GUI are organized into a containment hierarchy that is rooted at the top-level window. JPanel s and other Container s can be used to organize the components into a hierarchy according to function or some other criterion.
  • The top-level Swing classes—JApplet, JDialog, JFrame, and JWindow—use a content pane as their component container.
  • A GUI should minimize the number of input devices the user needs to manipulate, as well as the complexity the user needs to deal with. Certain forms of redundancy—such as two independent but complete sets of controls—are desirable because they make the interface more flexible and more widely applicable.
  • A layout manager is an object that manages the size and arrangement of the components in a container. The AWT and Swing provide a number of built-in layouts, including flow, border, grid, and box layouts.
  • A radio button is a toggle button that belongs to a group in which only one button from the group may be selected at the same time. A checkbox is a toggle button that always displays its state.
  • A well-designed interface should reduce the chance of user error and should make it as easy as possible to recover from errors when they do occur.

Solutions 13.10.3 Solutions to Self-Study Exercises

13.3 OBJECT-ORIENTED DESIGN: Model-View-Controller Architecture
13.3.2 Self-Study Exercise

Exercise 13.3.2. JButton MVC.
Solution.
How can a button still be considered a component under the MVC model? This is a good question. The JButton class acts as a wrapper class and hides the model-view-controller details (Fig. Figure 13.3.3). When you instantiate a JButton, you still get a single instance. Think of it this way: Your body consists of several systems that interact (internally) among themselves, but it’s still one body that other bodies interact with as a single object.

13.4 The Java Event Model
13.4.2 Self-Study Exercises

13.4.2.1. Listeners 1.
Solution.
A component can indeed be registered with more than one listener. For example, the ToggleButton that we defined in Chapter 4 has two listeners. The first is the button itself, which takes care of toggling the button’s label. The second is the frame in which the button is used, which takes care of handling whatever action the button is associated with.
13.4.2.2. Listeners 2.
Solution.
Some components can have two different kinds of listeners. For example, imagine a “sticky button” that works like this. When you click and release the button, it causes some action to take place, just like a normal button. When you click and hold the mouse button down, the button “sticks” to the cursor and you can then move it to a new location. This button would need listeners for ActionEvent, MouseEvent, and MouseMotionEvent.

13.5 CASE STUDY: Designing a Basic GUI
13.5.2 GUI Design Critique

Self-Study Exercises
13.5.2.1. Converter Rounding.
Solution.
To round a double you could use the Math.round() method. For example, suppose the number you want to round is d. Then the expression Math.round(100 * d)/100.0 will round to two decimal places. Alternatively, you could use the java.text.NumberFormat class. Both of these approaches were covered in Chapter 5.
13.5.2.2. Car Design Redundancy.
Solution.
Many cars today have cruise control as a alternative way to control the accelerator. Push buttons, usually located on the steering wheel, are used to speed up and slow down, so you can drive with your foot or your hand.

13.6 Containers and Layout Managers
13.6.5 Self-Study Exercises

Exercise 13.6.12. BorderLayout.
Solution.
As an alternative, a north-west-center border layout for the top-level window in the Converter might work. So might center-south-east and center-south-west. What makes these possible is the fact that the layout manager will use up space in any edge area that is not assigned a component.
Exercise 13.6.13. FlowLayout.
Solution.
A flow layout would not be appropriate for the control panel because you would have little control of where the convert button would be placed relative to the keypad.

13.7 Checkboxes, Radio Buttons, and Borders
13.7.6 Self-Study Exercise

Exercise 13.7.9. FlowLayout.
Solution.
Interface design disaster: My car uses the same kind of on/off switch for the headlights and the windshield wipers. One is a stem on the left side of the steering wheel, and the other is on a stem on the right side of the steering wheel. On more than one occasion, I’ve managed to turn off the headlights when I intended to turn on the wipers.

13.8 Menus and Scroll Panes
13.8.6 Self-Study Exercises

Exercise 13.8.8. Limit Cuts.
Solution.
Modify the addRecentCut() method so it limits the cuts stored in the vector to the last ten cuts. Solution: Check the size of the vector after inserting the cut. If it exceeds ten, remove the last element in the vector.
private void addRecentCut(String cut) {
  recentCuts.add(0,cut);
  if (recentCuts.size() > 10) { // If more than 10 cuts
    recentCuts.remove(10);  // remove oldest cut
  }
  cutsMenu.removeAll();
  for (int k = 0; k < recentCuts.size(); k++) {
    JMenuItem item =
      new JMenuItem((String) recentCuts.get(k));
    cutsMenu.add(item);
    item.addActionListener(this);
  }
} // addRecentCut()
Exercise 13.8.9. No Duplicate Cuts.
Solution.
Modify the addRecentCut() method so that it doesn’t duplicate cuts stored in the ArrayList. Solution: Use the indexOf() method to search for the cut. If it’s already there, don’t insert the cut.
private void addRecentCut(String cut) {
if (recentCuts.indexOf(cut) == -1) {// If not already cut
recentCuts.add(0,cut);
if (recentCuts.size() > 10) { // If more than 10 cuts
  recentCuts.removeElementAt(10); // remove oldest
}
cutsMenu.removeAll();
for (int k = 0; k < recentCuts.size(); k++) {
  JMenuItem item =
    new JMenuItem((String) recentCuts.get(k));
  cutsMenu.add(item);
  item.addActionListener(this);
  }
 } // if not already cut
} // addRecentCut()
Exercise 13.8.10. FileMenu Actions.
Solution.
No solution provided for this, but review Chapter 11 on Files.
You have attempted of activities on this page.