-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalendarPanel.java
More file actions
315 lines (274 loc) · 8.56 KB
/
CalendarPanel.java
File metadata and controls
315 lines (274 loc) · 8.56 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* * * * * * * * * * *\
* CalendarPanel.java
* Description: Used when running reports. Constructed with a JTextField and a JButton. When the button is clicked,
* a calendar is shown for user to selcect a date, the selected date populates the passed in text field.
*
* Date: 5/7/16
* @author Brandon Ballard
\* * * * * * * * * * */
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import static javax.swing.GroupLayout.Alignment.*;
import java.util.*;
import java.text.SimpleDateFormat;
class CalendarPanel extends JPanel implements ChangeListener, MouseListener
{
JPanel panel, monthYearPanel, container;
GroupLayout layout, layout2;
JSpinner monthSpinner, yearSpinner;
JTextField yearTF, field;
SpinnerListModel monthModel;
SpinnerNumberModel yearModel;
String month, currMonth, selectedDate, databaseDate;
int year, currYear, m, dayOfMonth, maxDays, v, selectedDay;
SimpleDateFormat yearFormat, monthFormat, dayFormat;
Calendar calendar;
JButton button;
String[] monthStrings = {"December", "November", "October", "September", "August", "July",
"June", "May", "April", "March", "February", "January"};
CalendarPanel(JTextField field, JButton button)
{
this.button = button;//this is the button the user presses to show the calendar
this.field = field;//this is the text field where the selected date will be placed
monthFormat = new SimpleDateFormat("MMMM");
yearFormat = new SimpleDateFormat("yyyy");
dayOfMonth = 1;
maxDays = v = selectedDay = 0;
m = Integer.parseInt(new SimpleDateFormat("M").format(new Date()));
maxDays = getMaxDays(Integer.parseInt(new SimpleDateFormat("M").format(new Date())));//returns how many days are in current month
currMonth = monthFormat.format(new Date());
currYear = Integer.parseInt(yearFormat.format(new Date()));
monthModel = new SpinnerListModel(monthStrings);
monthSpinner = new JSpinner(monthModel);
monthSpinner.addChangeListener(this);
yearModel = new SpinnerNumberModel(currYear, currYear - 100, currYear + 100, 1);
yearSpinner = new JSpinner(yearModel);
yearSpinner.addChangeListener(this);
JSpinner.NumberEditor editor = new JSpinner.NumberEditor(yearSpinner, "#");
yearSpinner.setEditor(editor);
calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, Integer.parseInt(new SimpleDateFormat("M").format(new Date())) - 1);
calendar.set(Calendar.YEAR, currYear);
calendar.set(Calendar.DAY_OF_MONTH, 1);
panel = new JPanel(new GridLayout(7,7));
panel.setMaximumSize(new Dimension(220, 170));
panel.setMinimumSize(new Dimension(220, 170));
panel.setPreferredSize(new Dimension(220, 170));
panel.add(new DayPanel("Sun"));
panel.add(new DayPanel("Mon"));
panel.add(new DayPanel("Tue"));
panel.add(new DayPanel("Wed"));
panel.add(new DayPanel("Thu"));
panel.add(new DayPanel("Fri"));
panel.add(new DayPanel("Sat"));
//Add day of month cells to calendar
for(int x = 0; x < 6 * 7; x++)
{
if(x < calendar.get(Calendar.DAY_OF_WEEK) - 1 || dayOfMonth > maxDays)
{
panel.add(new CellPanel(""));//Blank cell **there are a total of 42 cells and only 31 days in a month so not all cells can have a value
}
else
{
panel.add(new CellPanel(dayOfMonth, this, currMonth, (int)yearSpinner.getValue())).addMouseListener(this);//Cell with appropriate day of month
dayOfMonth++;
}
}
dayOfMonth = 1;
monthYearPanel = new JPanel();
monthYearPanel.setBackground(new Color(1, 0.1f, 0.1f).darker().darker());
layout2 = new GroupLayout(monthYearPanel);
monthYearPanel.setLayout(layout2);
layout2.setAutoCreateGaps(true);
layout2.setAutoCreateContainerGaps(true);
GroupLayout.SequentialGroup hGroup2 = layout2.createSequentialGroup();
hGroup2.addGroup(layout2.createParallelGroup().addComponent(yearSpinner));
hGroup2.addGroup(layout2.createParallelGroup().addComponent(monthSpinner));
layout2.setHorizontalGroup(hGroup2);
GroupLayout.SequentialGroup vGroup2 = layout2.createSequentialGroup();
vGroup2.addGroup(layout2.createParallelGroup(BASELINE).addComponent(yearSpinner).addComponent(monthSpinner));
layout2.setVerticalGroup(vGroup2);
container = new JPanel(new FlowLayout());
container.setBackground(Color.GRAY);
layout = new GroupLayout(container);
container.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
hGroup.addGroup(layout.createParallelGroup().addComponent(monthYearPanel).addComponent(panel));
layout.setHorizontalGroup(hGroup);
GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
vGroup.addGroup(layout.createParallelGroup(BASELINE).addComponent(monthYearPanel));
vGroup.addGroup(layout.createParallelGroup(BASELINE).addComponent(panel));
layout.setVerticalGroup(vGroup);
add(container);
monthSpinner.setValue(currMonth);
v++;
setVisible(false);
}
//Called when user changes the month or year on the calendar
public void stateChanged(ChangeEvent ce)
{
month = (String)monthSpinner.getValue();
year = (int)yearSpinner.getValue();
if( v >= 1)
{
updateCalendar();
}
}
//Returns the total number of days in the passed in month
int getMaxDays(int month)
{
if(month == 9 || month == 4 || month == 6 || month == 11)
{
return 30;
}
else if(month == 2)
{
if(!isLeapYear((int)(yearSpinner.getValue())))
{
return 28;
}
else
{
return 29;
}
}
else
{
return 31;
}
}
//Function name says it all, returns true if passed in year is a leap year, else returns false
public boolean isLeapYear(int year)
{
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
{
return true;
}
else
{
return false;
}
}
//Called anytime the user has changed year or month
void updateCalendar()
{
container.removeAll();
currMonth = (String)monthSpinner.getValue();
if(currMonth.equals("January"))
{
m = 1;
}
else if(currMonth.equals("February"))
{
m = 2;
}
else if(currMonth.equals("March"))
{
m = 3;
}
else if(currMonth.equals("April"))
{
m = 4;
}
else if(currMonth.equals("May"))
{
m = 5;
}
else if(currMonth.equals("June"))
{
m = 6;
}
else if(currMonth.equals("July"))
{
m = 7;
}
else if(currMonth.equals("August"))
{
m = 8;
}
else if(currMonth.equals("September"))
{
m = 9;
}
else if(currMonth.equals("October"))
{
m = 10;
}
else if(currMonth.equals("November"))
{
m = 11;
}
else if(currMonth.equals("December"))
{
m = 12;
}
else
{
m = 1;
}
maxDays = getMaxDays(m);
currYear = (int)yearSpinner.getValue();
calendar.set(Calendar.MONTH, m - 1);
calendar.set(Calendar.YEAR, currYear);
calendar.set(Calendar.DAY_OF_MONTH, 1);
panel = new JPanel(new GridLayout(7,7));
panel.setMaximumSize(new Dimension(220, 170));
panel.setMinimumSize(new Dimension(220, 170));
panel.setPreferredSize(new Dimension(220, 170));
panel.add(new DayPanel("Sun"));
panel.add(new DayPanel("Mon"));
panel.add(new DayPanel("Tue"));
panel.add(new DayPanel("Wed"));
panel.add(new DayPanel("Thu"));
panel.add(new DayPanel("Fri"));
panel.add(new DayPanel("Sat"));
for(int x = 0; x < 6 * 7; x++)
{
if(x < calendar.get(Calendar.DAY_OF_WEEK) - 1 || dayOfMonth > maxDays)
{
panel.add(new CellPanel(""));
}
else
{
panel.add(new CellPanel(dayOfMonth, this, (String)monthSpinner.getValue(), (int)yearSpinner.getValue())).addMouseListener(this);
dayOfMonth++;
}
}
dayOfMonth = 1;
layout = new GroupLayout(container);
container.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
hGroup.addGroup(layout.createParallelGroup().addComponent(monthYearPanel).addComponent(panel));
layout.setHorizontalGroup(hGroup);
GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
vGroup.addGroup(layout.createParallelGroup(BASELINE).addComponent(monthYearPanel));
vGroup.addGroup(layout.createParallelGroup(BASELINE).addComponent(panel));
layout.setVerticalGroup(vGroup);
}
public void mouseEntered(MouseEvent me)
{
}
public void mouseExited(MouseEvent me)
{
}
//Populates text field, hides calendar
public void mouseClicked(MouseEvent me)
{
field.setText(m + "/" + selectedDay + "/" + currYear);
databaseDate = (currYear + "/" + m + "/" + selectedDay);
setVisible(false);
button.setVisible(true);
}
public void mouseReleased(MouseEvent me)
{
}
public void mousePressed(MouseEvent me)
{
}
}