Create an application that stores people’s names and birthdays in a file (Fig. 25.49). The user creates a file and inputs each person’s first name, last name and birthday in the application. The information is then written to the file, as shown in the right image of Fig. 25.49. Note that only the month and day are written to the file.
a) Copying the template to your working directory. Copy the directory C:Examples Tutorial25ExercisesBirthdaySaver to your C:SimplyJava directory.
b) Opening the template file. Open the BirthdaySaver.java file in your text editor.
c) Viewing the PrintWriter instance variable. In lines 29–30, view the PrintWriter
instance variable named output that will be used to write output to a file.
d) Opening a JFileChooser dialog. For this exercise, you will define only the open- FileJButtonActionPerformed method (lines 160–163 in the template). Functional- ity for writing data to a file and closing a file is provided in the template. Starting on line 163, add and display a new JFileChooser to allow the user to select the file to which to write. Then, define an if statement that causes the method to be exited if the user clicks the Cancel JButton.
e) Validating the file name. Retrieve the file selected by the user (as a File object) and the name of the file. Create a File object, selectedFile
```
1 // BirthdaySaver.java
2 // This application allows the user to store the names and birthdays
3 // of people in a file.
4 import java.awt.*;
5 import java.awt.event.*;
6 import java.io.*;
7 import java.util.Date;
8 import javax.swing.*;
9
10 public class BirthdaySaver extends JFrame
11 {
12 // JLabel and JTextField for first name
13 private JLabel firstNameJLabel;
14 private JTextField firstNameJTextField;
15
16 // JLabel and JTextField for last name
17 private JLabel lastNameJLabel;
18 private JTextField lastNameJTextField;
19
20 // JLabel and JSpinner for birthday display
21 private JLabel birthdayJLabel;
22 private JSpinner birthdayJSpinner;
23
24 // JButtons to write to different files
25 private JButton openFileJButton;
26 private JButton enterJButton;
27 private JButton closeFileJButton;
28
29 // PrintWriter for writing data to a file
30 private PrintWriter output;
31
32 // no-argument constructor
33 public BirthdaySaver()
34 {
35 createUserInterface();
36 }
37
38 // create and position GUI components; register event handlers
39 private void createUserInterface()
40 {
41 // get content pane for attaching GUI components
42 Container contentPane = getContentPane();
43
44 // enable explicit positioning of GUI components
45 contentPane.setLayout( null );
46
47 // set up firstNameJLabel
48 firstNameJLabel = new JLabel();
49 firstNameJLabel.setBounds( 16, 8, 64, 23 );
50 firstNameJLabel.setText( "First Name:" );
51 contentPane.add( firstNameJLabel );
52
53 // set up firstNameJTextField
54 firstNameJTextField = new JTextField();
55 firstNameJTextField.setBounds( 96, 8, 100, 21 );
56 contentPane.add( firstNameJTextField );
57
58 // set up lastNameJLabel
59 lastNameJLabel = new JLabel();
60 lastNameJLabel.setBounds( 16, 40, 64, 23 );
61 lastNameJLabel.setText( "Last Name:" );
62 contentPane.add( lastNameJLabel );
63
64 // set up lastNameJTextField
65 lastNameJTextField = new JTextField();
66 lastNameJTextField.setBounds( 96, 40, 100, 21 );
67 contentPane.add( lastNameJTextField );
68
69 // set up birthdayJLabel
70 birthdayJLabel = new JLabel();
71 birthdayJLabel.setBounds( 16, 75, 56, 16 );
72 birthdayJLabel.setText( "Birthday:" );
73 contentPane.add( birthdayJLabel );
74
75 // set up birthdayJSpinner
76 birthdayJSpinner =
77 new JSpinner( new SpinnerDateModel() );
78 birthdayJSpinner.setBounds( 96, 72, 104, 23 );
79 birthdayJSpinner.setEditor(
80 new JSpinner.DateEditor(
81 birthdayJSpinner, "MM/dd/yyyy" ) );
82 contentPane.add( birthdayJSpinner );
83
84 // set up openFileJButton
85 openFileJButton = new JButton();
86 openFileJButton.setBounds( 237, 8, 95, 24 );
87 openFileJButton.setText( "Open File..." );
88 contentPane.add( openFileJButton );
89 openFileJButton.addActionListener(
90
91 new ActionListener() // anonymous inner class
92 {
93 // event handler called when openFileJButton is clicked
94 public void actionPerformed( ActionEvent event )
95 {
96 openFileJButtonActionPerformed( event );
97 }
98
99 } // end anonymous inner class
100
101 ); // end call to addActionListener
102
103 // set up enterJButton
104 enterJButton = new JButton();
105 enterJButton.setBounds( 237, 40, 95, 24 );
106 enterJButton.setText( "Enter" );
107 enterJButton.setEnabled( false );
108 contentPane.add( enterJButton );
109 enterJButton.addActionListener(
110
111 new ActionListener() // anonymous inner class
112 {
113 // event handler called when enterJButton is clicked
114 public void actionPerformed( ActionEvent event )
115 {
116 enterJButtonActionPerformed( event );
117 }
118
119 } // end anonymous inner class
120
121 ); // end call to addActionListener
122
123 // set up closeFileJButton
124 closeFileJButton = new JButton();
125 closeFileJButton.setBounds( 237, 72, 95, 24 );
126 closeFileJButton.setText( "Close File" );
127 closeFileJButton.setEnabled( false );
128 contentPane.add( closeFileJButton );
129 closeFileJButton.addActionListener(
130
131 new ActionListener() // anonymous inner class
132 {
133 // event handler called when closeFileJButton is pressed
134 public void actionPerformed( ActionEvent event )
135 {
136 closeFileJButtonActionPerformed( event );
137 }
138
139 } // end anonymous inner class
140
141 ); // end call to addActionListener
142
143 // set properties of application's window
144 setTitle( "Birthday Saver" ); // set title bar string
145 setSize( 350, 131 ); // set window size
146 setVisible( true ); // display window
147
148 } // end method createUserInterface
149
150 // reset input fields
151 private void clearUserInput()
152 {
153 firstNameJTextField.setText( "" );
154 lastNameJTextField.setText( "" );
155 birthdayJSpinner.setValue( new Date() );
156
157 } // end method clearUserInput
158
159 // enable user to select file to open
160 private void openFileJButtonActionPerformed( ActionEvent event )
161 {
162 // display file dialog so user can select file to open
163 JFileChooser fileChooser = new JFileChooser();
164 int result = fileChooser.showOpenDialog( this );
165
166 // if user clicked Cancel JButton on dialog, return
167 if ( result == JFileChooser.CANCEL_OPTION )
168 {
169 return; // exit method openFileJButtonActionPerformed
170 }
171
172 // obtain selected file
173 File selectedFile = fileChooser.getSelectedFile();
174
175 // obtain name of selected file
176 String fileName = selectedFile.getName();
177
178 // display error if file name invalid
179 if ( fileName.equals( "" ) )
180 {
181 JOptionPane.showMessageDialog( this, "Invalid file name.",
182 "Invalid File Name", JOptionPane.ERROR_MESSAGE );
183 }
184 else
185 {
186 // open file
187 try
188 {
189 FileWriter outputFile = new FileWriter(
190 selectedFile, true );
191 output = new PrintWriter( outputFile );
192 openFileJButton.setEnabled( false );
193 enterJButton.setEnabled( true );
194 closeFileJButton.setEnabled( true );
195 }
196
197 catch ( IOException exception )
198 {
199 JOptionPane.showMessageDialog( this,
200 "Error opening file.", "Error",
201 JOptionPane.ERROR_MESSAGE );
202 }
203
204 } // end else
205
206 } // end method openFileJButtonActionPerformed
207
208 // write data to file
209 private void enterJButtonActionPerformed( ActionEvent event )
210 {
211 // write user input to file
212 output.println( firstNameJTextField.getText() + " " +
213 lastNameJTextField.getText() );
214 String date = String.valueOf( birthdayJSpinner.getValue() );
215 output.println( date.substring( 4, 10 ) );
216
217 clearUserInput();
218
219 } // end method enterJButtonActionPerformed
220
221 // close file
222 private void closeFileJButtonActionPerformed( ActionEvent event )
223 {
224 // close PrintWriter
225 output.close();
226
227 openFileJButton.setEnabled( true );
228 enterJButton.setEnabled( false );
229 closeFileJButton.setEnabled( false );
230
231 } // end method closeFileJButtonActionPerformed
232
233 // main method
234 public static void main( String args[] )
235 {
236 BirthdaySaver application = new BirthdaySaver();
237 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
238
239 } // end method main
240
241 } // end class BirthdaySaver
```
You might also like to view...
A customer comes into the computer store where you are working looking for a cable with which you are unfamiliar. The best course of action is for you to ____
A) Tell the customer you do not know what that is. B) Ask the customer to explain to you the purpose of the cable. C) Tell the customer there is no such cable. D) Refer the customer to the service desk. E) Ask your supervisor for assistance.
Network administrators and support personnel commonly use the ____ utility to verify connectivity between two points.
A. Path B. Ping C. SYN D. Transport
A ____ contains massive amounts of information that can be electronically searched for specific facts or documents.
A. data warehouse B. data cube C. data plant D. data wholesaler
Google commands over what percentage of all online searches?
a. 54% b. 25% c. 64% d. 83%