(To-Do List Application) Use a JTextArea as a to-do list. Enter each item in a JTextField, and add it to the JTextArea by clicking a JButton. The item should be displayed in a numbered list as in Fig. 8.27. To do this, you will need JTextArea method getLineCount, which returns the number of lines in a JTextArea. The following statement assigns the number of lines displayed in the outputJTextArea to int variable counter:

```

int counter = outputJTextArea.getLineCount();

```



```

a) Copying the template to your working directory. Copy the directory C:Examples Tutorial08ExercisesToDoList to your C:SimplyJava directory.

b) Opening the template file. Open the ToDoList.java file in your text editor.

c) Adding code to the event handler for the Add JButton. Add code to the addJBut- tonActionPerformed event handler (which begins in line 82) to obtain the number of lines displayed in the outputJTextArea. Get the user input from taskJText- Field append it the outputJTextArea with a line number in front of it. After the input is added to the outputJTextArea, clear the taskJTextField.

d) Saving the application. Save your modified source code file.

e) Opening the Command Prompt window and changing directories. Open the Com- mand Prompt window by selecting Start > Programs > Accessories > Command Prompt. Change to your working directory by typing cd C:SimplyJavaToDoList.

f) C


```
1 // Exercise 8.17: ToDoList.java
2 // Creates a To-Do List based on user input.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 public class ToDoList extends JFrame
8 {
9 // JLabel and JTextField to input a task
10 private JLabel taskJLabel;
11 private JTextField taskJTextField;
12
13 // JButton to initiate adding a task to the to-do list
14 private JButton addJButton;
15
16 // JTextArea to display the to-do list
17 private JTextArea outputJTextArea;
18
19 // no-argument constructor
20 public ToDoList()
21 {
22 createUserInterface();
23 }
24
25 // create and position GUI components; register event handlers
26 public void createUserInterface()
27 {
28 // get content pane for attaching GUI components
29 Container contentPane = getContentPane();
30
31 // enable explicit positioning of GUI components
32 contentPane.setLayout( null );
33
34 // set up taskJLabel
35 taskJLabel = new JLabel();
36 taskJLabel.setBounds( 16, 8, 40, 26 );
37 taskJLabel.setText( "Task:" );
38 contentPane.add( taskJLabel );
39
40 // set up taskJTextField
41 taskJTextField = new JTextField();
42 taskJTextField.setBounds( 65, 8, 100, 26 );
43 taskJTextField.setHorizontalAlignment( JTextField.RIGHT );
44 contentPane.add( taskJTextField );
45
46 // set up addJButton
47 addJButton = new JButton( "Add" );
48 addJButton.setBounds( 183, 8, 75, 26 );
49 contentPane.add( addJButton );
50 addJButton.addActionListener(
51
52 new ActionListener() // anonymous inner class
53 {
54 // event handler called when user clicks addJButton
55 public void actionPerformed ( ActionEvent event )
56 {
57 addJButtonActionPerformed( event );
58 }
59
60 } // end anonymous inner class
61
62 ); // end call to addActionListener
63
64 // set up outputJTextArea
65 outputJTextArea = new JTextArea();
66 outputJTextArea.setEditable( false );
67
68 // set up JScrollPane to allow JTextArea scrolling
69 JScrollPane scrollJScrollPane =
70 new JScrollPane( outputJTextArea );
71 scrollJScrollPane.setBounds( 16, 48, 242, 95 );
72 contentPane.add( scrollJScrollPane );
73
74 // set properties of application’s window
75 setTitle( "To Do List" ); // set title bar text
76 setSize( 280, 188 ); // set window's size
77 setVisible( true ); // display window
78
79 } // end method createUserInterface
80
81 // called when user clicks addJButton
82 public void addJButtonActionPerformed( ActionEvent event )
83 {
84 // get line count in outputJTextArea
85 int counter = outputJTextArea.getLineCount();
86
87 // display task
88 outputJTextArea.append(
89 counter + ". " + taskJTextField.getText() + "\n" );
90
91 taskJTextField.setText( "" ); // clear taskJTextField
92
93 } // end method addJButtonActionPerformed
94
95 // main method
96 public static void main( String args[] )
97 {
98 ToDoList application = new ToDoList();
99 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
100
101 } // end method main
102
103 } // end class ToDoList
```

Computer Science & Information Technology

You might also like to view...

Which of the following types of software made it cheaper and easier to start Web 2.0 companies?

a. Vaporware. b. Proprietary. c. Open source. d. None of the above.

Computer Science & Information Technology

What is displayed when the button is clicked on?

``` Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim a, b as String Dim x as Integer a = "How now brown cow." b = "brown" x = FindIt(a, b) txtBox.Text = CStr(x) End Sub Function FindIt(z1 as String, z2 as String) As Integer Dim x as Integer x = z1.IndexOf(z2) End Function ``` (A) "How now" (B) 8 (C) 0 (D) An error (E) None of the above

Computer Science & Information Technology

The command at l

a: lists jobs that are scheduled for later run b: displays the access time for each file in the current directory c: there is no such command d: displays files in a long format e: lists all the large files in the current directory

Computer Science & Information Technology

A(n) ____ is a form control that allows a user to choose one or more items from a list of items.

A. radio button B. option box C. check box D. choice box

Computer Science & Information Technology