- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddAnnivForm.java
More file actions
Latest commit
259 lines (218 loc) · 6.52 KB
/
Copy pathAddAnnivForm.java
File metadata and controls
259 lines (218 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
importjava.io.*;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importjava.util.*;
@SuppressWarnings("all")
publicclassAddAnnivFormextendsJFrameimplementsActionListener
{
privatestaticString [] Months={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
privatestaticString[] Days={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
privateJComboBoxcho_Period,cho_Month,cho_Day;
privateJTextFieldtxt_Entryid,txt_Name,txt_Year;
privateJButtoncmd_Add,cmd_Cancel,cmd_New;
publicAddAnnivForm(Stringfname)
{
super(fname);
cho_Period=newJComboBox();
txt_Entryid=newJTextField(5);
txt_Name=newJTextField(10);
txt_Year=newJTextField(5);
txt_Entryid.setEnabled(false);
cmd_Add=newJButton("Save");
cmd_Cancel=newJButton("Back");
cmd_New=newJButton("New");
Vectorv=newVector();
Vectorv2=newVector();
for(inti=0;i<31;i++)
{
v.addElement(Days[i]);
}
//Add values in day combobox.
cho_Day=newJComboBox(v);
for(inti=0;i<12;i++)
{
v2.addElement(Months[i]);
}
//Add values in month combobox.
cho_Month=newJComboBox(v2);
JPanelpanel=newJPanel();
JPanelpanel1=newJPanel();
JPanelpanel2=newJPanel();
JPanelpanel3=newJPanel();
JPanelpanel4=newJPanel();
JPanelpanel5=newJPanel();
JPanelpanel6=newJPanel();
JPaneltpanel=newJPanel();
panel1.setLayout(newFlowLayout());
panel.setLayout(newFlowLayout());
panel.add(newJLabel("B. R. Enterprises"));
panel1.add(newJLabel("Anniversary Entry Form"));
tpanel.setLayout(newBorderLayout());
tpanel.add(panel,BorderLayout.NORTH);
tpanel.add(panel1,BorderLayout.SOUTH);
panel2.setLayout(newGridLayout(2,2,0,5));
panel2.add(newJLabel("Entry ID : "));
panel2.add(txt_Entryid);
panel2.add(newJLabel("Name : "));
panel2.add(txt_Name);
panel3.setLayout(newFlowLayout(FlowLayout.LEFT));
panel3.add(newJLabel("Enter Date of Ann. : "));
panel3.add(newJLabel("Day"));
panel3.add(cho_Day);
panel3.add(newJLabel("Month"));
panel3.add(cho_Month);
panel3.add(newJLabel("Year"));
panel3.add(txt_Year);
panel4.setLayout(newFlowLayout());
panel4.add(cmd_New);
panel4.add(cmd_Add);
panel4.add(cmd_Cancel);
JPanelmpanel=newJPanel();
mpanel.setLayout(newGridLayout(4,1,15,5));
mpanel.add(tpanel);
mpanel.add(panel2);
mpanel.add(panel3);
mpanel.add(panel4);
getContentPane().setLayout(newFlowLayout());
getContentPane().add(mpanel);
txt_Entryid.setEnabled(false);
cmd_New.setEnabled(false);
cmd_Add.addActionListener(this);
cmd_Cancel.addActionListener(this);
cmd_New.addActionListener(this);
addWindowListener(newMyWindowAdapter());
}
publicvoidactionPerformed(ActionEventae)
{
//If user clicks on Add button.
if(ae.getSource()==cmd_Add)
{
//Check all values entered.
if(checkValues()==true)
{
//Get unique entry id for new record.
inteid=getCount();
txt_Entryid.setText(" "+eid);
try
{
//Open Annivers file to append a new entry.
BufferedWriterbr=newBufferedWriter(newFileWriter("Annivers",true));
Stringstr=newString();
str=txt_Entryid.getText().trim()+"*"+txt_Name.getText().trim()+"*"+(String)cho_Day.getSelectedItem()+"*"+(String)cho_Month.getSelectedItem()+"*"+txt_Year.getText().trim()+"\n";
br.write(str);
br.close();
cmd_Add.setEnabled(false);
cmd_New.setEnabled(true);
} catch(FileNotFoundExceptionfnfe)
{
displayMesg("Annivers file not found");
}
catch(IOExceptionioe)
{
displayMesg("I/O error");
}
}
}
//If user clicks on New button.
elseif(ae.getSource()==cmd_New)
{
txt_Entryid.setText("");
txt_Name.setText("");
txt_Year.setText("");
cho_Day.setSelectedIndex(0);
cho_Month.setSelectedIndex(0);
cmd_New.setEnabled(false);
cmd_Add.setEnabled(true);
}
//If user clicks on Back button.
elseif(ae.getSource()==cmd_Cancel)
{
AddFormaddform=newAddForm("Add Contact Form");
addform.setSize(300,250);
addform.setResizable(false);
addform.setVisible(true);
dispose();
}
}
//Checks if all the textfields are provided with values.
publicbooleancheckValues()
{
if((txt_Name.getText().trim().length()==0) || (txt_Year.getText().trim().length()==0))
{
displayMesg("Please Fill Each Entry");
returnfalse;
}
if (txt_Year.getText().length()>4 || txt_Year.getText().length()<4)
{
displayMesg("Year Size Should Be 4");
returnfalse;
}
if((txt_Year.getText().trim().length()!=0))
{
try
{
Long.parseLong(txt_Year.getText().trim());
displayMesg("Record Saved");
returntrue;
}
catch (Exceptione)
{
displayMesg("Check Year Entry");
returnfalse;
}
}
else
{
displayMesg("Record Saved");
returntrue;
}
}
//Inner class to implement windowClosing() method.
classMyWindowAdapterextendsWindowAdapter
{
publicvoidwindowClosing(WindowEventwe)
{
AddFormaddform=newAddForm("Add Contact Form");
addform.setSize(250,250);
addform.setResizable(false);
addform.setVisible(true);
dispose();
}
}
//getCount() method returns a new Entryid for the new record.
intgetCount()
{
intcount=0;
try
{
BufferedReaderbr=newBufferedReader(newFileReader("Annivers"));
Stringstr;
//Traverse to the last record in the file and reads the Entryid of the last record into
//the variable count and assigns new Entryid by incrementing count by 1.
while((str=br.readLine())!=null)
{
StringTokenizertoken=newStringTokenizer(str,"*");
Stringeid=token.nextToken();
count=Integer.parseInt(eid);
}
count++;
br.close();
} catch(FileNotFoundExceptionfnfe)
{
count++;
}
catch(IOExceptionioe)
{
displayMesg("I/O error");
}
returncount;
}
publicvoiddisplayMesg(Stringm)
{
MyDialogdialog=newMyDialog(this,m,true);
dialog.setLocation(this.getX(),this.getY());
dialog.setSize(300,100);
dialog.setVisible(true);
}
}