(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
```
You might also like to view...
The Windows feature that removes a newly installed, improperly working driver and replaces it with the last one that worked is called ________
Fill in the blank(s) with correct word
A ________ is a program that appears to be a legitimate program but is actually malicious
A) rootkit B) logic bomb C) botnet D) Trojan horse