This repository was archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalendarPane.java
More file actions
149 lines (125 loc) · 4.02 KB
/
CalendarPane.java
File metadata and controls
149 lines (125 loc) · 4.02 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
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.TextAlignment;
public class CalendarPane extends BorderPane {
// The header label
private Label lblHeader = new Label();
// Maximum number of labels to display day names and days
private Label[] lblDay = new Label[49];
private Calendar calendar;
private int month; // The specified month
private int year; // The specified year
private Locale locale = Locale.CHINA;
public CalendarPane() {
// Create labels for displaying days
for (int i = 0; i < 49; i++) {
lblDay[i] = new Label();
lblDay[i].setTextAlignment(TextAlignment.RIGHT);
}
showDayNames(); // Display day names for the locale
GridPane dayPane = new GridPane();
dayPane.setAlignment(Pos.CENTER);
dayPane.setHgap(10);
dayPane.setVgap(10);
for (int i = 0; i < 49; i++) {
dayPane.add(lblDay[i], i % 7, i / 7);
}
// Place header and calendar body in the pane
this.setTop(lblHeader);
BorderPane.setAlignment(lblHeader, Pos.CENTER);
this.setCenter(dayPane);
// Set current month and year
calendar = new GregorianCalendar();
month = calendar.get(Calendar.MONTH);
year = calendar.get(Calendar.YEAR);
updateCalendar();
// Show calendar
showHeader();
showDays();
}
/** Update the day names based on locale */
private void showDayNames() {
DateFormatSymbols dfs = new DateFormatSymbols(locale);
String dayNames[] = dfs.getWeekdays();
// jlblDay[0], jlblDay[1], ..., jlblDay[6] for day names
for (int i = 0; i < 7; i++) {
lblDay[i].setText(dayNames[i + 1]);
}
}
/** Update the header based on locale */
private void showHeader() {
SimpleDateFormat sdf =
new SimpleDateFormat("MMMM yyyy", locale);
String header = sdf.format(calendar.getTime());
lblHeader.setText(header);
}
public void showDays() {
// Get the day of the first day in a month
int startingDayOfMonth = calendar.get(Calendar.DAY_OF_WEEK);
// Fill the calendar with the days before this month
Calendar cloneCalendar = (Calendar) calendar.clone();
cloneCalendar.add(Calendar.DATE, -1); // Becomes preceding month
int daysInPrecedingMonth = cloneCalendar.getActualMaximum(
Calendar.DAY_OF_MONTH);
for (int i = 0; i < startingDayOfMonth - 1; i++) {
lblDay[i + 7].setTextFill(Color.LIGHTGRAY);
lblDay[i + 7].setText(daysInPrecedingMonth
- startingDayOfMonth + 2 + i + "");
}
// Display days of this month
int daysInCurrentMonth = calendar.getActualMaximum(
Calendar.DAY_OF_MONTH);
for (int i = 1; i <= daysInCurrentMonth; i++) {
lblDay[i - 2 + startingDayOfMonth + 7].setTextFill(Color.BLACK);
lblDay[i - 2 + startingDayOfMonth + 7].setText(i + "");
}
// Fill the calendar with the days after this month
int j = 1;
for (int i = daysInCurrentMonth - 1 + startingDayOfMonth + 7;
i < 49; i++) {
lblDay[i].setTextFill(Color.LIGHTGRAY);
lblDay[i].setText(j++ + "");
}
}
/** Set the calendar to the first day of the
* specified month and year
*/
public void updateCalendar() {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DATE, 1);
}
public int getMonth() {
return month;
}
public void setMonth(int newMonth) {
month = newMonth;
updateCalendar();
showHeader();
showDays();
}
public int getYear() {
return year;
}
public void setYear(int newYear) {
year = newYear;
updateCalendar();
showHeader();
showDays();
}
public void setLocale(Locale locale) {
this.locale = locale;
updateCalendar();
showDayNames();
showHeader();
showDays();
}
}