Write an application that allows users to add appointments to an appointment book that uses the Java Speech API (Fig. 28.18). When the user clicks the Add JButton, the application adds the name, time and date of the appointment to three ArrayLists, respectively. When the user clicks the Get Appointments JButton, the application speaks the time and date of the appointment that the user has with that person.



a) Copying the template to your working directory. Copy the C:Examples Tutorial28ExercisesAppointmentBook directory to your C:SimplyJava direc- tory.

b) Opening the template file. Open the AppointmentBook.java file in your text editor. c) Importing the Java Speech API packages. Import the javax.speech and the

javax.speech.synthesis packages.

d) Declaring instance variables. At line 48, declare three instance variables of type ArrayList to store the date, time and person with which the user has an appoint- ment. Declare an instance variable of type Synthesizer, which will be used to speak text.

e) Creating a Synthesizer object. Inside the AppointmentBook constructor, create a

Synthesizer object, allocate the resource and get the synthesizer ready to speak.

f) Adding code to the addJButtonActionPerformed method. Find the addJButtonAc- tionPerformed method, which immediately follows createUserInterface. Add code to the addJButtonActionPerformed method so that the information provided by the user is


1 //
2 //
3 //
4 import java.awt.*;
5 import java.awt.event.*;
6 import java.util.*;
7 import javax.swing.*;
8 import javax.speech.*;
9 import javax.speech.synthesis.*;
10
11 public class AppointmentBook extends JFrame
12 {
13 // JLabel and JTextField for the name of the person that the
14 // appointment will be with
15 private JLabel appointmentWithJLabel;
16 private JTextField appointmentWithJTextField;
17
18 // JLabel, JSpinner and DateEditor for the date of the
19 // appointment
20 private JLabel appointmentDateJLabel;
21 private JSpinner appointmentDateJSpinner;
22 private JSpinner.DateEditor dateFormat;
23
24 // JLabel and JTextField for the time of the appointment
25 private JLabel appointmentTimeJLabel;
26 private JTextField appointmentTimeJTextField;
27
28 // JButton to add an appointment
29 private JButton addJButton;
30
31 // JButton to get appointment
32 private JButton getAppointmentJButton;
33
34 // arrays to store the month
35 private final String[] threeLettersMonths = {
36 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
37 "Sep", "Oct", "Nov", "Dec" };
38 private final String[] months = { "January", "February", "March",
39 "April", "May", "June", "July", "August", "September",
40 "October", "November", "December" };
41
42 // arrays to store the dates
43 private final String[] threeLettersDays =
44 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
45 private final String[] days = { "Sunday", "Monday", "Tuesday",
46 "Wednesday", "Thursday", "Friday", "Saturday" };
47
48 // ArrayLists to store the date, time and person with which
49 // the user has an appointment.
50 private ArrayList dateArrayList = new ArrayList();
51 private ArrayList timeArrayList = new ArrayList();
52 private ArrayList personArrayList = new ArrayList();
53
54 // Synthesizer to speak text
55 private Synthesizer speechSynthesizer;
56
57 // no-argument constructor
58 public AppointmentBook()
59 {
60 // initialize Synthesizer
61 try
62 {
63 // create SynthesizerModeDesc for FreeTTS synthesizer.
64 SynthesizerModeDesc descriptor = new SynthesizerModeDesc(
65 "Unlimited domain FreeTTS Speech Synthesizer " +
66 "from Sun Labs", null, Locale.US, Boolean.FALSE, null);
67
68 // create a Synthesizer
69 speechSynthesizer = Central.createSynthesizer( descriptor );
70
71 // Synthesizer created successfully
72 if ( speechSynthesizer != null )
73 {
74 // get synthesizer ready to speak
75 speechSynthesizer.allocate();
76 speechSynthesizer.resume();
77
78 // get synthesizer properties
79 SynthesizerProperties properties =
80 speechSynthesizer.getSynthesizerProperties();
81
82 // set up speaking rate
83 properties.setSpeakingRate( 100.0f );
84
85 } // end if
86
87 else
88 {
89
90 System.err.println( "Synthesizer creation failed." System.exit( 1 );
91 }
92
93 } // end try
94
95 catch ( Exception myException )
96 {
97 myException.printStackTrace();
98 }
99 createUserInterface(); // set up GUI
100
101
102 } // end constructor
103
104 // create and position GUI components; register event handler
105 private void createUserInterface()
106 {
107 // get content pane for attaching GUI components
108 Container contentPane = getContentPane();
109
110 // enable explicit positioning of GUI components
111 contentPane.setLayout( null );
112
113 // set up appointmentWithJLabel
114 appointmentWithJLabel = new JLabel();
115 appointmentWithJLabel.setBounds( 16, 16, 106, 23 );
116 appointmentWithJLabel.setText( "Appointment With:" );
117 contentPane.add( appointmentWithJLabel );
118
119 // set up appointmentWithJTextField
120 appointmentWithJTextField = new JTextField();
121 appointmentWithJTextField.setBounds( 128, 16, 136, 21 );
122 appointmentWithJTextField.setText( "" );
123 contentPane.add( appointmentWithJTextField );
124
125 // set up appointmentDateJLabel
126 appointmentDateJLabel = new JLabel();
127 appointmentDateJLabel.setBounds( 16, 48, 106, 23 );
128 appointmentDateJLabel.setText( "Appointment Date:" );
129 contentPane.add( appointmentDateJLabel );
130
131 // set up appointmentDateJSpinner
132 SpinnerModel model = new SpinnerDateModel();
133 appointmentDateJSpinner = new JSpinner ( model );
134 dateFormat = new JSpinner.DateEditor( appointmentDateJSpinner,
135 "MM/dd/yyyy" );
136 appointmentDateJSpinner.setEditor( dateFormat );
137 appointmentDateJSpinner.setBounds( 128, 48, 136, 21 );
138 contentPane.add( appointmentDateJSpinner );
139
140 // set up appointmentTimeJLabel
141 appointmentTimeJLabel = new JLabel();
142 appointmentTimeJLabel.setBounds( 16, 80, 106, 23 );
143 appointmentTimeJLabel.setText( "Appointment Time:" );
144 contentPane.add( appointmentTimeJLabel );
145
146 // set up appointmentTimeJTextField
147 appointmentTimeJTextField = new JTextField();
148 appointmentTimeJTextField.setBounds( 128, 80, 136, 21 );
149 appointmentTimeJTextField.setText( "" );
150 contentPane.add( appointmentTimeJTextField );
151
152 // set up addJButton
153 addJButton = new JButton();
154 addJButton.setBounds( 20, 112, 75, 23 );
155 addJButton.setText( "Add" );
156 contentPane.add( addJButton );
157 addJButton.addActionListener(
158
159 new ActionListener() // anonymous inner class
160 {
161 // event handler called when addJButton is clicked
162 public void actionPerformed( ActionEvent event )
163 {
164 addJButtonActionPerformed( event );
165 }
166
167 } // end anonymous inner class
168
169 ); // end call to addActionListener
170
171 // set up getAppointmentJButton
172 getAppointmentJButton = new JButton();
173 getAppointmentJButton.setBounds( 100, 112, 150, 23 );
174 getAppointmentJButton.setText( "Get Appointment" );
175 contentPane.add( getAppointmentJButton );
176 getAppointmentJButton.addActionListener(
177
178 new ActionListener() // anonymous inner class
179 {
180 // event handler called when getAppointmentJButton
181 // is clicked
182 public void actionPerformed( ActionEvent event )
183 {
184 getAppointmentJButtonActionPerformed( event );
185 }
186
187 } // end anonymous inner class
188
189 ); // end call to addActionListener
190
191 // set properties of the application's window
192 setTitle( "Appointment Book" ); // set title bar string
193 setSize( 280, 176 ); // set window size
194 setVisible( true ); // display window
195
196 // ensure synthesizer is cleaned up
197 // when user closes application
198 addWindowListener(
199
200 new WindowAdapter() // anonymous inner class
201 {
202 public void windowClosing( WindowEvent event )
203 {
204 frameWindowClosing( event );
205 }
206
207 } // end anonymous inner class
208
209 ); // end addWindowListener
210
211 } // end method createUserInterface
212
213 // add an appointment
214 private void addJButtonActionPerformed( ActionEvent event )
215 {
216 // either appointmentWithJTextField or
217 // appointmentTimeJTextField is empty
218 if ( appointmentWithJTextField.getText().equals( "" ) ||
219 appointmentTimeJTextField.getText().equals( "" ) )
220 {
221 JOptionPane.showMessageDialog( null,
222 "You left one or more fields empty above",
223 "Empty Field", JOptionPane.WARNING_MESSAGE );
224 }
225 else // both JTextFields are filled in
226 {
171 // set up getAppointmentJButton
172 getAppointmentJButton = new JButton();
173 getAppointmentJButton.setBounds( 100, 112, 150, 23 );
174 getAppointmentJButton.setText( "Get Appointment" );
175 contentPane.add( getAppointmentJButton );
176 getAppointmentJButton.addActionListener(
177
178 new ActionListener() // anonymous inner class
179 {
180 // event handler called when getAppointmentJButton
181 // is clicked
182 public void actionPerformed( ActionEvent event )
183 {
184 getAppointmentJButtonActionPerformed( event );
185 }
186
187 } // end anonymous inner class
188
189 ); // end call to addActionListener
190
191 // set properties of the application's window
192 setTitle( "Appointment Book" ); // set title bar string
193 setSize( 280, 176 ); // set window size
194 setVisible( true ); // display window
195
196 // ensure synthesizer is cleaned up
197 // when user closes application
198 addWindowListener(
199
200 new WindowAdapter() // anonymous inner class
201 {
202 public void windowClosing( WindowEvent event )
203 {
204 frameWindowClosing( event );
205 }
206
207 } // end anonymous inner class
208
209 ); // end addWindowListener
210
211 } // end method createUserInterface
212
213 // add an appointment
214 private void addJButtonActionPerformed( ActionEvent event )
215

Computer Science & Information Technology

You might also like to view...

The first form of ____ uses the equality criteria.

A. adjacent_search B. adjacent_query C. adjacent_find D. adjacent_criteria

Computer Science & Information Technology

What is the first step in generating a realistic three-dimensional image?

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

Answer the following statement(s) true (T) or false (F)

1. Nearly 20 years ago, Photoshop 1.0 was released exclusively for the Windows operating system. _________________________ 2. The higher the resolution of an image, the sharper the image appears. _________________________ 3. The mathematical instructions in a(n) bitmap graphic define the lines and curves that make up the graphic. _________________________ 4. The status bar is found at the lower-left corner of your workspace. _________________________

Computer Science & Information Technology

The end of an event or use case is when the system is at rest in a consistent state.

True or False

Computer Science & Information Technology