100% found this document useful (1 vote)
438 views4 pages

Chapter 13 Review Question Answers

This document contains the answers to review questions for Chapter 13 of the textbook "Starting Out with Java: From Control Structures through Objects, 6/e". The questions cover multiple choice, true/false, finding errors in code examples, algorithm workbench exercises, and short answer questions related to Java GUI components like JLists, JButtons, JSliders and more.

Uploaded by

spiff spaceman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
438 views4 pages

Chapter 13 Review Question Answers

This document contains the answers to review questions for Chapter 13 of the textbook "Starting Out with Java: From Control Structures through Objects, 6/e". The questions cover multiple choice, true/false, finding errors in code examples, algorithm workbench exercises, and short answer questions related to Java GUI components like JLists, JButtons, JSliders and more.

Uploaded by

spiff spaceman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Gaddis: Starting Out with Java: From Control Structures through Objects, 6/e 1

Starting Out with Java - From Control Structures through Objects


Answers to Review Questions

Chapter 13

Multiple Choice and True/False


1. d
2. c
3. b
4. d
5. a
6. c
7. c
8. a
9. b
10. d
11. c
12. a
13. a
14. c
15. b
16. d
17. a
18. c
19. b
20. c
21. a
22. True
23. False
24. True
25. False
26. False
27. True
28. True
29. True
30. False
31. True
32. True
33. False
34. False
35. True

Find the Error


1. The argument false should have been passed to the setEditable method.
2. The code should read:
list.setBorder(BorderFactory.createLineBorder(Color.black,1));
Gaddis: Starting Out with Java: From Control Structures through Objects, 6/e 2

3. You should pass list as an argument to the JScrollPane constructor:


JScrollPane scrollPane = new JScrollPane(list);

4. The statement should read:


String selectedName = (String) nameBox.getSelectedItem();

5. The second statement should read:


label.setIcon(image);

6. You pass a JMenu object as an argument to the JMenuBar object's add method:
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);

7. The statement should read:


JTextArea textArea = new JTextArea (5, 20);

Algorithm Workbench
1.
JTextField textField = new JTextField(20);
textField.setEditable(false);

2.
String[]days={"Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday",
"Sunday"};
JListdayList=newJList(days);

3.
dayList.setVisibleRowCount(4);
JScrollPanescrollPane=newJScrollPane(dayList);

4.

selection=(String)myList.getSelectedValue();

5.

selectionIndex=myComboBox.getSelectedIndex();

6. ImageIcon image = new ImageIcon("dog.jpg");


JLabel label = new JLabel(image);

7. ImageIcon image = new ImageIcon("picture.gif");


label.setIcon(image);
Gaddis: Starting Out with Java: From Control Structures through Objects, 6/e 3

8. JButton openButton = new JButton("Open File");


openButton.setMnemonic(KeyEvent.VK_O);
openButton.setToolTipText("This button opens a file");

9. JFileChooser fileChooser = new JFileChooser();


int status = fileChooser.showOpenDialog(null);
if (status == JFileChooser.APPROVE_OPTION)
{
File selectedFile = fileChooser.getSelectedFile();
String filename = selectedFile.getPath();
}

10.
JTextAreatextInput=JTextArea(10,15);
JScrollPanescrollPane=newJScrollPane(textInput);
textInput.setLineWrap(true);
textInput.setWrapStyleWord(true);

11.

//CreateanOpenmenuitem.
JMenuItemopenItem=newJMenuItem("Open");
openItem.setMnemonic(KeyEvent.VK_O);
openItem.addActionListener(newOpenListener());

//CreateaPrintmenuitem.
JMenuItemprintItem=newJMenuItem("Print");
printItem.setMnemonic(KeyEvent.VK_P);
printItem.addActionListener(newPrintListener());

//CreateanExitmenuitem.
JMenuItemexitItem=newJMenuItem("Exit");
exitItem.setMnemonic(KeyEvent.VK_X);
exitItem.addActionListener(newExitListener());

//CreateaJMenuobjectfortheFilemenu.
JMenufileMenu=newJMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);

//AddthemenuitemstotheFilemenu.
fileMenu.add(openItem);
fileMenu.add(printItem);
fileMenu.add(exitItem);

//Createthemenubar.
JMenuBarmenuBar=newJMenuBar();
Gaddis: Starting Out with Java: From Control Structures through Objects, 6/e 4

//Addthefilemenutothemenubar.
menuBar.add(fileMenu);

//Setthewindow'smenubar.
setJMenuBar(menuBar);

12.
JSliderslider=newJSlider(JSlider.HORIZONTAL,
0,1000,500);
slider.setMajorTickSpacing(100);
slider.setMinorTickSpacing(25);
slider.setPaintTickMarks(true);
slider.setPaintLabels(true);

Short Answer
1. Single selection mode
2. JComboBox
3. An uneditable combo box combines a button with a list, and allows the user to
only select items from its list. An editable combo box combines a text field and a
list. In addition to selecting items from the list, the user may also type input into
the text field. The default type of combo box is uneditable.
4. You can pass the text as an argument to the constructor, and then pass an
ImageIcon object to the setIcon method. Another way is to pass the text, an
ImageIcon object, and an int specifying horizontal alignment to the JLabel
constructor.
5. A mnemonic is a key on the keyboard that you press in combination with the Alt
key to quickly access a component such as a button. When you assign a
mnemonic to a button, the user can click the button by holding down the Alt key
and pressing the mnemonic key.
6. The first occurrence of that letter appears underlined.
7. A tool tip is text that is displayed in a small box when the user holds the mouse
cursor over a component. The box usually gives a short description of what the
component does.
8. Add them to a ButtonGroup object.
9. The item is deselected, which causes the check mark to disappear. The checked
menu item component also generates an action event.
10. Dialog, DialogInput, SansSerif, Serif, and Monospaced
11. Because, as the user moves the JSlider component's knob, it will only take on
values within its established range.
12. Metal, Motif, and Windows

You might also like