Create an application that allows users to add items to a daily task list. The application’s GUI should appear as in Fig. 14.16. The tasks should be placed in a JTextArea, one task per line. The application should also display the number of tasks to be performed. Use method String.valueOf to display the number of tasks in numberJText- Field.
a) Copying the template to your working directory. Copy the C:Examples Tutorial14ExercisesTaskList directory to your C:SimplyJava directory.
b) Opening the template file. Open the TaskList.java file in your text editor.
c) Declaring an instance variable. At line 24, declare instance variable counter of type int and initialize its value to 0. Add a comment before you declare the instance vari- able.
d) Adding code to the Add Task JButton’s actionPerformed event handler. At line
108, add code to the addTaskJButtonActionPerformed event handler. This event
handler should display the user input in the taskListJTextArea, increment the instance variable created in the previous step, update the numberJTextField that displays the number of tasks and clear the user input from the taskJTextField. Use method String.valueOf to display the number of tasks in the numberJTextField.
e) Saving the application. Save your modified source code file.
f) Opening the C
```
1 // TaskList.java
2 // Creates a task list based on user input.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 public class TaskList extends JFrame
8 {
9 // JLabel and JTextField to display task
10 private JLabel taskJLabel;
11 private JTextField taskJTextField;
12
13 // JButton to enter task
14 private JButton addTaskJButton;
15
16 // JLabel and JTextArea to display entered tasks
17 private JLabel taskListJLabel;
18 private JTextArea taskListJTextArea;
19
20 // JLabel and JTextField to display number of entered tasks
21 private JLabel numberJLabel;
22 private JTextField numberJTextField;
23
24 // declare instance variable to store number of tasks
25 private int counter = 0;
26
27 // no-argument constructor
28 public TaskList()
29 {
30 createUserInterface();
31 }
32
33 // create and position GUI components; register event handlers
34 private void createUserInterface()
35 {
36 // get content pane for attaching GUI components
37 Container contentPane = getContentPane();
38
39 // enable explicit positioning of GUI components
40 contentPane.setLayout( null );
41
42 // set up taskJLabel
43 taskJLabel = new JLabel();
44 taskJLabel.setBounds( 16, 16, 32, 24 );
45 taskJLabel.setText( "Task:" );
46 contentPane.add( taskJLabel );
47
48 // set up taskJTextField
49 taskJTextField = new JTextField();
50 taskJTextField.setBounds( 56, 16, 128, 24 );
51 taskJTextField.setHorizontalAlignment( JTextField.RIGHT );
52 contentPane.add( taskJTextField );
53
54 // set up addTaskJButton
55 addTaskJButton = new JButton();
56 addTaskJButton.setBounds( 200, 16, 148, 24 );
57 addTaskJButton.setText( "Add Task" );
58 contentPane.add( addTaskJButton );
59 addTaskJButton.addActionListener(
60
61 new ActionListener() // anonymous inner class
62 {
63 // event handler called when addTaskJButton is clicked
64 public void actionPerformed ( ActionEvent event )
65 {
66 addTaskJButtonActionPerformed( event );
67 }
68
69 } // end anonymous inner class
70
71 ); // end call to addActionListener
72
73 // set up taskListJLabel
74 taskListJLabel = new JLabel();
75 taskListJLabel.setBounds( 16, 64, 100, 24 );
76 taskListJLabel.setText( "Task list:" );
77 contentPane.add( taskListJLabel );
78
79 // set up taskListJTextArea
80 taskListJTextArea = new JTextArea();
81 taskListJTextArea.setBounds( 16, 90, 168, 108 );
82 taskListJTextArea.setEditable( false );
83 contentPane.add( taskListJTextArea );
84
85 // set up numberJLabel
86 numberJLabel = new JLabel();
87 numberJLabel.setBounds( 200, 104, 116, 24 );
88 numberJLabel.setText( "Number of Tasks:" );
89 contentPane.add( numberJLabel );
90
91 // set up numberJTextField
92 numberJTextField = new JTextField();
93 numberJTextField.setBounds( 316, 104, 32, 24 );
94 numberJTextField.setEditable ( false );
95 numberJTextField.setHorizontalAlignment( JTextField.CENTER );
96 contentPane.add( numberJTextField );
97
98 // set properties of application’s window
99 setTitle( "Task List" ); // set title bar string
100 setSize( 375, 240 ); // set window size
101 setVisible( true ); // display window
102
103 } // end method createUserInterface
104
105 // display task list and task number
106 public void addTaskJButtonActionPerformed( ActionEvent event )
107 {
108 // add task to taskListJTextArea
109 taskListJTextArea.append( taskJTextField.getText() + "\n" );
110
111 counter++; // increment task number
112
113 // display number of tasks
114 numberJTextField.setText( String.valueOf( counter ) );
115
116 taskJTextField.setText( "" ); // clear taskJTextField
117
118 } // end method addJButtonActionPerformed
119
120 // main method
121 public static void main( String args[] )
122 {
123 TaskList application = new TaskList();
124 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
125
126 } // end method main
127
128 } // end class TaskList
```
You might also like to view...
A(n) ____________________ chart often is used to illustrate changes in data over time.
Fill in the blank(s) with the appropriate word(s).
Windows 8 and Office 2013 have fully integrated the touch environment
Indicate whether the statement is true or false.
The ____ of a movie is the storyline or plan for the dramatic flow of the movie.
A. plot B. shot list C. frame D. point of view
Worms don't require human interaction to spread
Indicate whether the statement is true or false.