[ Team LiB ] Previous Section Next Section

11.16 Exercises

Exercise 11-1. Take a look again at Figure 11-7. Write a class that produces a sample layout like GridBagLayoutPane, without using the GridBagLayout layout manager. You'll probably want to use BorderLayout and the Box container or BoxLayout layout manager.

Exercise 11-2. The ScribblePane class of Example 11-13 draws all of its lines using the foreground property. If this property changes, the color of all lines changes. Modify the class so that it remembers the setting of foreground when each PolyLine was drawn, and uses these retained colors in its paintComponent( ) method. With this modification, changes to the foreground property will not affect the currently displayed lines, only lines subsequently drawn.

Exercise 11-3. The ItemChooser class allows items to be specified only when the component is created. Add methods that allow items to be added and removed. Make the methods work regardless of the presentation type in use.

Exercise 11-4. The ScribbleApp application defines and uses a ColorAction class to allow the user to select the current drawing color. Add a LineWidthAction class that lets the user select the line width. To accomplish this, give the ScribblePane component a setLineWidth( ) method so that the line width can be varied.

Exercise 11-5. One shortcoming of the FontChooser class is that when you call setSelectedFont( ), the ItemChooser components are not updated to match the current font. Modify FontChooser so that it does update these selections. You'll probably want to modify ItemChooser so that, in addition to its setSelectedIndex( ) method, it also has setSelectedLabel( ) and setSelectedValue( ) methods that specify the selected item by label or by value.

Exercise 11-6.Modify ShowBean so that it has a Containment Hierarchy.. . item in the File menu. When the user selects this item, the program should display a ComponentTree component (see Example 11-20) in a separate window, to display the containment hierarchy of the currently displayed bean.

Exercise 11-7. A common feature of web browsers is a Go menu that lists the last 10 or 15 URLs that have been visited and provides a quick way to revisit those sites. Add this feature to the WebBrowser class. Note that WebBrowser already tracks its browsing history. You need to add a Go menu to the JMenuBar that is read from the GUIResourceBundle. The contents of the Go menu depends on the current browsing history. One technique is to change the contents of the menu each time the browser visits a new page. Alternatively, you can use a MenuListener object to receive notification just before the menu is popped up, and then, from the menuSelected( ) method of the listener, add the appropriate items to the menu.

Exercise 11-8. The GUIResourceBundle class and the ResourceParser extension interface provide a powerful mechanism for describing a GUI in a properties file. Implement more ResourceParser classes to support other resource types. In particular, write parsers for the ImageIcon and KeyStroke classes; support for these two types is required to fully support the ActionParser class.

Exercise 11-9. Modify the ShowBean class to use the ThemeManager class (with an appropriate properties file) and the Themes menu it can create. Ideally, you should make the Themes menu a submenu of the Look and Feel menu the program already displays. Modify ThemeManager to retain the user's preferred theme using the java.util.prefs package, like LookAndFeelPrefs does.

    [ Team LiB ] Previous Section Next Section