Introduction to Java Programming, Seventh Edition, Y. Daniel Liang

Chapter 35 MVC and Swing MVC Components


Section 35.2 MVC
1  The component for storing and handling data, known as _________, contains the actual contents of the component.

A. a model
B. a view
C. a controller

2  Which of the following statements are true?

A. The model-view-controller (MVC) approach is a way of developing components by separating data storage and handling from the visual representation of the data.
B. The model-view-controller (MVC) approach makes multiple views possible so that data can be shared through the same model.
C. The model-view-controller (MVC) approach simplifies the task of writing complex applications and makes the components scalable and easy to maintain. Changes can be made to the view without affecting the model, and vice versa.
D. Models and views are synchronized to ensure that a view displays the data consistently.

Section 35.4 Swing Model-View-Controller Architecture
3  Which of the following statements are true?

A. Each Swing user interface component (except some containers and dialog boxes such as JPanel, JSplitPane, JFileChooser, and JColorChooser) has a property named model that refers to its data model.
B. The data model is defined in an interface whose name ends with Model. For example, the model for button component is ButtonModel.
C. Most model interfaces have a default implementation class that is commonly named DefaultX, where X is its model interface name. For example, the default implementation class for ButtonModel is DefaultButtonModel.
D. For convenience, most Swing components contain some properties of their models, and these properties can be accessed and modified directly from the component without knowing the existence of the model.
E. It is unnecessary to use the models for the simple Swing components such as JButton, JToggleButton, JCheckBox, JRadioButton, JTextField, and JTextArea, because the frequently used properties in their models are also in these components.

4  Analyze the following code.
public class Test {
   public static void main(String[] args) {
     javax.swing.JButton jbt1 = new javax.swing.JButton();
     javax.swing.JButton jbt2 = new javax.swing.JButton();

     // Create a new button model
     javax.swing.ButtonModel model =
       new javax.swing.DefaultButtonModel();

     // Set properties in the model
     model.setActionCommand("OK");
     model.setMnemonic('O');

     // Assign the model to the button
     jbt1.setModel(model);
     jbt2.setModel(model);
   
     jbt2.setActionCommand("Cancel");

     // Display the property values from the component
     System.out.println("jbt1's actionCommand is " + jbt1.getActionCommand());
     System.out.println("jbt1's mnemonic is " + jbt1.getMnemonic());
     System.out.println("jbt2's actionCommand is " + jbt2.getActionCommand());
     System.out.println("jbt2's mnemonic is " + jbt2.getMnemonic());
   }
}

A. jbt1 and jbt2 are the same object.
B. jbt1 and jbt2 have the the same model.
C. jbt1 and jbt2 have the same actionCommand property.
D. jbt1 and jbt2 have the same mnemonic property.

Section 35.5 JSpinner
5  If you create a JSpinner object without specifying a model, the spinner displays _______.

A. a sequence of integers
B. a sequence of double values
C. a sequence of positive integers
D. a sequence of non-negative integers

6  JSpinner fires _________ when a new value is selected.

A. java.awt.ActionEvent
B. java.awt.event.ItemEvent
C. javax.swing.event.ChangeEvent
D. javax.swing.event.AdjustmentEvent

7  A listener of a JSpinner must implement _________.

A. the java.awt.ActionListener interface
B. the java.awt.ItemListener interface
C. javax.swing.event.ChangeListener
D. javax.swing.event.AdjustmentListener

8  JSpinner has the following methods:

A. getNextValue()
B. getPreviousValue()
C. getValue()
D. setValue(Object)

9  __________ uses a java.util.List to store a sequence of custom defined data in the model.

A. SpinnerListModel
B. SpinnerNumberModel
C. SpinnerDateModel
D. SpinnerModel

10  You can create a SpinnerListModel using __________.

A. new SpinnerListModel()
B. new SpinnerListModel(new ArrayList())
C. new SpinnerListModel(new int[10])
D. new SpinnerListModel(new Integer[10])

11  You can create a SpinnerNumberModel using __________.

A. new SpinnerNumberModel()
B. new SpinnerNumberModel(0, -10, 10, 1)
C. new SpinnerNumberModel(0, -10, 10, 0.5)
D. new SpinnerNumberModel(new Integer(0), new Integer(-10), new Integer(10), new Integer(1))

12  Which of the following are the properties in SpinnerNumberModel?

A. maximum
B. minimum
C. stepSize
D. nextValue
E. previousValue

13  Which of the following are the properties in SpinnerDateModel?

A. start
B. end
C. nextValue
D. previousValue

Section 35.7 JList
14  You can create a JList using __________.

A. new JList()
B. new JList(new String[]{"red", "green", "blue"})
C. new JList(new Vector())
D. new JList(new DefaultListModel());

15 
The layoutOrientation property in JList specifies how elements are laid out in a list with the following possible values:

A. JList.VERTICAL
B. JList.HORIZONTAL_WRAP
C. JList.VERTICAL_WRAP
D. JList.NO_WRAP
E. Default is JList.VERTICAL

16  The selectionMode property in JList specifies how items in a list can be selected with the following possible values:

A. ListSelectionModel.SINGLE_SELECTION
B. ListSelectionModel.SINGLE_INTERVAL_SELECTION
C. ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
D. Default is ListSelectionModel.MULTIPLE_INTERVAL_SELECTION

17  _________ are the properties in JList.

A. visibleRowCount
B. model
C. fixedCellHeight
D. fixedCellWidth
E. selectionMode

18  JList fires _________ when a new item is selected.

A. java.awt.ActionEvent
B. java.awt.event.ItemEvent
C. javax.swing.event.ChangeEvent
D. javax.swing.event.AdjustmentEvent

19  A listener of a JList must implement _________.

A. the java.awt.ActionListener interface
B. the java.awt.ItemListener interface
C. javax.swing.event.ChangeListener
D. javax.swing.event.AdjustmentListener

20  The data in DefaultListModel are stored in ___________.

A. an array.
B. an ArrayList
C. a LinkedList
D. a Vector

21  The data in DefaultListModel are stored in ___________.

A. an array.
B. an ArrayList
C. a LinkedList
D. a Vector

22  The DefaultListModel class contains the methods __________.

A. clear()
B. add(Object)
C. remove(Object)
D. contains(Object)
E. size()

23  DefaultListCellRenderer can display _________.

A. only a string
B. only an icon
C. both string and icon
D. a string or an icon

Section 35.10 JComboBox
24  You can create a JComboBox using __________.

A. new JComboBox()
B. new JList(new String[]{"red", "green", "blue"})
C. new JList(new Vector())
D. new JList(new DefaultComboBoxModel());

25  _________ are the properties in JList.

A. visibleRowCount
B. maximumRowCount
C. editable
D. itemCount
E. selectedIndex

26  JComboBox fires _________ when a new item is selected.

A. java.awt.ActionEvent
B. java.awt.event.ItemEvent
C. javax.swing.event.ChangeEvent
D. javax.swing.event.AdjustmentEvent

27  JComboBox contains the methods ___________.
A. for adding an item (addItem(Object))
B. for inserting an item (insertItemAt(Object item, int index)
C. for removing an item (removeItemAt(int index))
D. for removing all items (removeAllItems())