I have to make a fully function calendar for my into to Java class and I am stuck. I am using GregorianCalendar
to get the current day, month, year, etc. I am using JButton
s to populate the 42 fields that fill out the calendar, and having the days of the month post on each individual JButton
. I am having a problem getting the first day of the month (July) to fall in the correct JButton
. The calendar is recognizing the month as July, and the year as 2012, but it's putting the 1st on Monday instead of Sunday, and is off by one week. Here is my code, thanks for any help!
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.GregorianCalendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Calendar extends JFrame {
public Calendar() {
this.setSize(800, 600);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
this.setTitle("Calendar");
JPanel borderPanel = new JPanel();
borderPanel.setLayout(new BorderLayout());
getContentPane().add(borderPanel);
// Initialize GregorianCalendar
GregorianCalendar cal = new GregorianCalendar();
int currYear = cal.get(GregorianCalendar.YEAR);
int currDOM = cal.get(GregorianCalendar.DAY_OF_MONTH);
int currMnth = cal.get(GregorianCalendar.MONTH);
String month2 = "";
switch (currMnth) {
case 0:
month2 = "January";
break;
case 1:
month2 = "February";
break;
case 2:
month2 = "March";
break;
case 3:
month2 = "April";
break;
case 4:
month2 = "May";
break;
case 5:
month2 = "June";
break;
case 6:
month2 = "July";
break;
case 7:
month2 = "August";
break;
case 8:
month2 = "September";
break;
case 9:
month2 = "October";
break;
case 10:
month2 = "November";
break;
case 11:
month2 = "December";
break;
}
int nod, som;
nod = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH); //Number of Days
som = cal.get(GregorianCalendar.DAY_OF_WEEK_IN_MONTH); // Start of the Month
// Button Layout
final int ROWS = 6;
final int COLS = 7;
JButton[][] days;
JPanel calendar = new JPanel();
calendar.setLayout(new GridLayout(6, 7));
days = new JButton[ROWS][COLS];
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
days[row][col] = new JButton("");
calendar.add(days[row][col]);
}
}
// Add Some Number to the Buttons
int[] tableMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int startDay = som;
int daysInMonth = tableMonth[currMnth];
for (int i = 0; i < daysInMonth; i++) {
days[(startDay + i) / 7][(startDay + i) % 7].setText("" + (i + 1));
}
borderPanel.add(calendar, BorderLayout.CENTER);
// North Panel - Current Month / Prev & Next Button
JPanel month = new JPanel();
month.add(new JButton("Previous"));
month.add(new JLabel(month2));
month.add(new JButton("Next"));
borderPanel.add(month, BorderLayout.NORTH);
// West Panel - Current Date
JPanel year = new JPanel();
year.add(new JButton("The Year is: " + currYear));
borderPanel.add(year, BorderLayout.SOUTH);
// South Panel - Current Year
JPanel today = new JPanel();
today.add(new JButton("Today is: " + month2 + " " + currDOM + ", " + currYear));
borderPanel.add(today, BorderLayout.WEST);
}
public static void main(String[] args) {
new Calendar();
}
}