Chapter 18  Review Exercise Solutions
R18. 1
A label is a display area for a short text string or an image, or both. The text cannot be edited.
A text field holds a single line of editable text. A text area displays multiple lines of editable text.
R18.2
The append method is declared in the JTextArea class.
The setText method is declared in the JTextComponent class.
The getGraphics method is declared in the JComponent class.
R18.3
Yes, you can. You have to set the frame's layout (actually the frame's content pane layout) to FlowLayout:

   frame.setLayout(new FlowLayout());

Then, you can just add the user interface components, one by one:

   frame.add(new JButton("A"));
   frame.add(new JButton("B"));
R18.4
A layout manager is an object that arranges components inside a container.
The advantage of a layout manager is that the layout works even if the size of the components or the container changes. That can happen because components have different sizes in different look and feel implementations, or simply because the user resizes the container.
R18.5
If you place a single button in the CENTER area, it grows to fill the entire center area.
R18.6
If you place multiple buttons in the SOUTH area, they are put on top of each other. Only the last one shows up.
R18.7
If you omit the second parameter of the add method when adding a component to a container that uses a border layout, then the component is added to the center.
R18.8
The second button appears nested (inside) the first button.
R18.9
The drawback of the grid layout is that the first and second column have the same size (because each cell gets the same size in a grid layout). It would be nicer if the first column was much narrower, so that there would be more room for the sliders.
Unfortunately, that isn't easy to achieve with the basic layouts. You can use (3,1) grid layout with 3 flow layouts, each of which contains a label and a slider. But then the sliders won't line up. Or you can use a border layout, put one (3,1) grid layout to hold the labels to the West and another grid layout to hold the sliders in the center. That will probably work OK, because labels and sliders have approximately the same height.
The best way to achieve a good layout is to use the GridBagLayout, but that layout manager is quite complex and is not used in this book.
R18.10
The GridBagLayout, like the GridLayout, arranges components in a grid of rows and columns. But, in the GridBagLayout the rows and columns do not have to have the same size, a component can span several cells, and components do not have to fill the entire area. All this comes at the cost of increased complexity.
R18.11
When creating a check box or radio button, you can specify an icon to use in the constructor:
   new JRadioButton("JRadioButton", new ImageIcon("icon.gif"));
   new JCheckBox("JCheckBox", new ImageIcon("icon.gif"));

You cannot set an icon in a JComboBox component.
R18.12
Radio buttons are used to select exactly one of a number of options.
Check boxes are used to select any combination of a number of options.
R18.13
The button group for radio buttons is responsible for turning the previously selected button off when another button is selected. That ensures that only one radio button from the group is selected at any time.
You don't need a button group for check boxes because in a group of check boxes, it is OK to have several check boxes selected.
R18.14
The menu bar is the horizontal bar at the top of a window.
A menu is a vertical arrangement of submenus and menu items.
A menu item is an individual item inside a menu. Only menu items generate action events.
R18.15
The constructor with no parameters produces a slider with a range of min=0, max=100. Simply replacing the sliders in our program with default sliders would not have worked—there would be no way to specify white (red=green=blue=255). The maximum value (red=green=blue=100) would be a dark gray.

It would have been possible to fix the program by scaling the slider values from the range [0,100] to the range [0,255], but that would have been more work than using the other constructor.
R18.16
You construct a vertical slider like this:
JSlider mySlider = new JSlider(SwingConstants.VERTICAL, min, max, value);
You can also use JSlider.VERTICAL because JSlider implements the SwingConstants interface.
R18.17
Action events describe one-time changes, such as button clicks. Change events describe continuous changes, such as moving a JSlider. In the case of a JComboBox, the events generated are one-time changes, not continuous changes.
R18.18
Use the JList component to show a list of items, with several items visible at the same time.
R18.19
The Swing documentation lists the following components:
JButton
JCheckBox
JCheckBoxMenuItem
JColorChooser
JComboBox
JComponent
JDesktopPane
JDialog
JEditorPane
JFileChooser
JFormattedTextField
JFrame
JInternalFrame
JLabel
JLayeredPane
JList
JMenu
JMenuBar
JMenuItem
JOptionPane
JPanel
JPasswordField
JPopupMenu
JProgressBar
JRadioButton
JRadioButtonMenuItem
JRootPane
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolBar
JToolTip
JTree
JViewport
JWindow
Not all of them correspond to visible components, but about 30 of them do.
R18.20
The following statistics are valid for Java 6.
Here are the 31 methods of JProgressBar:
addChangeListener(ChangeListener l)
createChangeListener()
fireStateChanged()
getAccessibleContext()
getChangeListeners()
getMaximum()
getMinimum()
getModel()
getOrientation()
getPercentComplete()
getString()
getUI()
getUIClassID()
getValue()
isBorderPainted()
isIndeterminate()
isStringPainted()
paintBorder(Graphics g)
paramString()
removeChangeListener(ChangeListener l)
setBorderPainted(boolean b)
setIndeterminate(boolean newValue)
setMaximum(int n)
setMinimum(int n)
setModel(BoundedRangeModel newModel)
setOrientation(int newOrientation)
setString(String s)
setStringPainted(boolean b)
setUI(ProgressBarUI ui)
setValue(int n)
updateUI()
In addition, JProgressBar inherits
•	130 methods from JComponent
•	57 methods from Container
•	150 methods from Component
•	10 methods from Object
All together, the class has 378 methods!