Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to show calendar of all months in swings  RSS feed

 
Praveen Kumar Anekalmath
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am new to Swing Application.
I want to show calendar of 12 months in single page one after other based on Year entered. How can i approach this in Swings? Please help...
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
work on producing a calendar for a single month.
when you have that right, put the criteria into a method that returns a JPanel.
now create a another JPanel, GridLayout (6,2) (2,6) (4,3) (3,4) (whatever),
and add the 12 panels to it, one for each month
 
Praveen Kumar Anekalmath
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael... But these months calendar should change according to Year entered..How can i approach it...?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Praveen Kumar Anekalmath:
Thanks Michael... But these months calendar should change according to Year entered..How can i approach it...?


The GregorianCalendar has all the required methods you will need to come up with a solution. Read the API documentation carefully as it is quiet descriptive. In case you come up with problems, post some code so that people can help you sort it out.
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How I did this once:
- two panels: one for weekday labels, one for day labels

First panel has a GridLayout(1, 7) layout and displays all days of the week.

Second panel has a GridLayout(0, 7). It has 31* JLabels added, in rows of 7. Each JLabel has the text of that day, or null / empty string if not used for the month (like 31 for Februari).

Now when you change the year, you changes the text of the weekday labels to match, fix the text for all labels as necessary (usually only necessary for February).



Another approach I also used is with the weekdays at fixed locations, and the first day switching around. It works similarly as the previous, but with some changes (date is the name of the Calendar):
- The number of labels is greater: date.getMaximum(Calendar.DAY_OF_MONTH) + Calendar.SATURDAY - Calendar.SUNDAY to ensure that the entire month will fit regardless of the number of days and the start of the month.
- The first X labels have an empty text; X is the difference of the day of the week of the 1st day and Sunday (start = date.get(Calendar.DAY_OF_WEEK) - Calendar.SUNDAY). From there on, you set the text until you reach date.getActualMaximum(Calendar.DAY_OF_MONTH).

This last one also allows changing the month, although you won't need that.


*Actually, date.getMaximum(Calendar.DAY_OF_MONTH), where date is the Calendar object.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!