Gregorian Calendar - Problems
Hello... I am working on a project for my intro to Java class and I am stuck. We have to design a functioning calendar, and I am using GregorianCalendar to pull my Current Day, Month, Year, etc. For some reason the month is off by 1 month though (6 instead of 7). Also, I would much rather return the name of the month (July instead of 6) but I can't seem to figure it out. Lastly, I can't seem to get the number of days to fall on the buttons in the proper place. I know I'm asking a lot, but this assignment is really hard, and I'm trying to get as much help as I can because I am stuck...
Here is my code...
Code :
public class Calendar extends JFrame {
public Calendar() {
this.setSize(640,480);
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);
int nod, som;
nod = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH); //Number of Days
som = cal.get(GregorianCalendar.DAY_OF_WEEK); // 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 JButton("The Month is: " + currMnth));
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: " + currMnth + "/" + currDOM + "/" + currYear));
borderPanel.add(today, BorderLayout.WEST);
}
public static void main(String[] args) {
new Calendar();
}
}
Thank you so much for any help!
Re: Gregorian Calendar - Problems
As for the name of the month, a simple switch statement should do it
As for the month thing, I'm getting that too.
I think that it is supposed to return 6.
Constant Field Values (Java Platform SE 7 )
January is 0.
int month = cal.get(GregorianCalendar.MONTH);
String month2 = "";
switch (month)
{
case 0:
month2 = "January";
break;
case 1:
month2 = "February";
break;
case 2:
month2 = "March";
break;
etc
}
Quote:
Originally Posted by BobDole6395
Lastly, I can't seem to get the number of days to fall on the buttons in the proper place.
What do you mean exactly by that?
Re: Gregorian Calendar - Problems
Quote:
What do you mean exactly by that?
The days of the month don't fall on the proper blocks. For example, July first was a Sunday, but it is falling on Tuesday. I think something is wrong with my for loop, but I can't see it.
Re: Gregorian Calendar - Problems
cal.get(GregorianCalendar.DAY_OF_WEEK);
is returning 3 for today.
I assume you meant to get the start day of the month.
Maybe you should use
cal.get(GregorianCalendar.DAY_OF_WEEK_IN_MONTH);
for figuring out the day_of_week/day_in_month correspondence.
public static final int DAY_OF_WEEK_IN_MONTH
Re: Gregorian Calendar - Problems
Thanks so much! I got the month to return with July instead of 6 thanks to the switch. I am not quite getting the calendar to fill in properly, I tried the DAY_OF_WEEK_IN_MONTH but it didn't help, or I did it wrong. I swear this stuff is way to hard for a 100 level intro to Java, but I gotta get it done, lol... Thanks again!
Re: Gregorian Calendar - Problems
Okay... so I've got everything working in terms of populating the buttons with the calendar. I am trying to setup the ActionListener for the prev and next buttons and I can't get it to work... well, its working, if I hit the button and do a println it updates the month +1 or -1 in my console, but the calendar itself is not refreshing. So, any ideas would be greatly appreciated!!!
Code :
public class Calendar extends JFrame {
static JButton btnNext, btnPrev;
static int currYear, currDOM, currentMonth;
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);
currentMonth = currMnth;
String StringMnth = "";
switch(currMnth) {
case 0: StringMnth = "January"; break;
case 1: StringMnth = "February"; break;
case 2: StringMnth = "March"; break;
case 3: StringMnth = "April"; break;
case 4: StringMnth = "May"; break;
case 5: StringMnth = "June"; break;
case 6: StringMnth = "July"; break;
case 7: StringMnth = "August"; break;
case 8: StringMnth = "September"; break;
case 9: StringMnth = "October"; break;
case 10: StringMnth = "November"; break;
case 11: StringMnth = "December"; break;
}
int nod, som;
cal.set(GregorianCalendar.DATE, 1);
nod = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH); //Number of Days
som = (cal.get(GregorianCalendar.DAY_OF_WEEK) - GregorianCalendar.SUNDAY + 7)%7; // 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
btnNext = new JButton(">>");
btnPrev = new JButton("<<");
JPanel month = new JPanel();
month.add(btnPrev);
month.add(new JLabel(StringMnth));
month.add(btnNext);
borderPanel.add(month, BorderLayout.NORTH);
//Listeners
btnPrev.addActionListener(new btnPrev_Action());
btnNext.addActionListener(new btnNext_Action());
// 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: " + StringMnth + " " + currDOM + ", " + currYear));
borderPanel.add(today, BorderLayout.WEST);
}
static class btnNext_Action implements ActionListener{
public void actionPerformed (ActionEvent e){
currentMonth += 1;
System.out.print(currentMonth);
}
}
static class btnPrev_Action implements ActionListener{
public void actionPerformed (ActionEvent e){
currentMonth -= 1;
System.out.print(currentMonth);
}
}
public static void main(String[] args) {
new Calendar();
}
}
Re: Gregorian Calendar - Problems
Anyone? I am stuck on the ActionListener... I can't seem to figure this out and it's driving me nuts!
Re: Gregorian Calendar - Problems
Changing currentMonth will not trigger the GUI to recreate the calendar. You will need to figure out a way to recreate the GUI when you change the month (perhaps by moving some of the GUI creation code into a method that you can call from the actionListener).