Computer Program Reading Comprehension Project
U of T Computer Program Reading Comprehension Project
JList
This problem set contains three independent programs,

  JListSample.java
  JListPanel.java 
  JListDo.java
  
listed in the order of difficulty. All three programs extend JApplet and contain objects from the JList class, which is a visual list of items.

Because this problem set contains ideas you are likely not familiar with, we do not assume that the readers have prior knowledge of these topics. Indeed, the purpose of the question set is to ask the readers to read completely new programs and suggest some ways to approach them. (Note that this by no means implies that a reader's approach is incorrect, or that the suggested approach is better.) Please do not read the source code until being instructed by the questions. Also, do not worry if you do not fully understand a question or a solution.

As mentioned before, this question set involves many advanced topics. A detailed discussion of these topics are beyond the scope of this exercise. Interested readers are encouraged to consult the Java online documentation.

The Java API provides classes for creating graphical user interfaces, graphics, and images. In this problem set, we will refer to these classes as GUI classes for simplicity. Also item and row are used interchangeably here to refer to options in a list.

Download files: JListSample.java, JListPanel.java, JListDo.java
Areas: Applet



JListSample.java
1/* University of Alberta (http://www.cs.ualberta.ca/) */
2/* CMPUT114(2003-2004, Fall) http://www-csfy.cs.ualberta.ca/~c114/F03 */
3
4import java.awt.*;
5import javax.swing.*;
6import java.applet.*;
7
8public class JListSample extends JApplet {
9    public void init() {
10        this.getContentPane().add(this.getList());
Displaying 10 of 22 lines
View full source


JListPanel.java
1/* CMPUT114(2003-2004, Fall) http://www-csfy.cs.ualberta.ca/~c114/F03 */
2
3import java.awt.*;
4import javax.swing.*;
5import java.applet.*;
6
7public class JListPanel extends JApplet {
8
9    public void init() {
10
Displaying 10 of 41 lines
View full source


JListDo.java
1/* CMPUT114(2003-2004, Fall) http://www-csfy.cs.ualberta.ca/~c114/F03 */
2
3import java.awt.*;
4import java.awt.event.*;
5import javax.swing.*;
6import java.applet.*;
7
8public class JListDo extends JApplet implements ActionListener {
9
10    private JLabel item;
Displaying 10 of 59 lines
View full source


Question 1
Please read JListSample.java.

What methods from the JList class are used in JListSample.java? How can you tell that they are from JList?

Hints Solution

Question 2
Can you guess the functionality of the method below?
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
Hints Solution

Question 3
Can you guess the functionality of setVisibleRowCount(2)?
Solution

Question 4
In the init() method, we have the statement
this.getContentPane().add(this.getList());
Is the this in this.getContentPane() used the same way as the this in this.getList()?
Hints Solution

Question 5
Are all the packages in the import list necessary?
Hints Solution

Question 6
Does your prior knowledge in programming help you to understand JListSample? Which kind of programming knowledge is it? Writing GUI code using Java? Writing GUI code using another language? Programming in general? Which parts of JListSample do you find particularly hard to comprehend?
Hints

Question 7
Please read JListPanel.java.

Can you guess what JListPanel does? How much can you conclude after reading the program only ONCE?

Hints Solution

Question 8
Does your understanding of JListSample help?
Hints

Question 9
What is GridLayout? Can you tell its nature (a class, a method, etc.) by reading the code?
Hints Solution

Question 10
Run JListSample and JListPanel. Compare the lists in them. The lists in JListPanel have scroll bars. Which part of the code in JListPanel.java causes this difference?
Hints Solution

Question 11
Do JListSample and JListPanel respond to user input?
Hints Solution

Question 12
Please read JListDo.java.

Unlike the first two programs, JListDo is interactive, that is it responds to user input. Can you guess which part of the code provides this functionality?

Hints Solution

Question 13
Can you guess which GUI component takes the user input?
Hints Solution

Question 14
What does addButton() do? Is the button actually added to the applet?
Hints Solution

Question 15
How many times is the method addButton() called? What does it accomplish each time? Is it necessary to call it in init()?
Hints Solution

Question 16
Assume you were a programmer that was assigned the task of debugging an online shopping cart program implemented in Java. All of JListSample, JListPanel, and JListDo were part of the online program. If the three programs appeared to be unrelated to the errors in the main program, what level of understanding would you aim at? If the three programs appeared to cause the malfunction of the main program, what level of understanding would you aim at?
Solution