A J2ME Calendar for all your Canvas!
You know that J2ME support for Canvas is quite ridiculous.. One list, some form items, and stop. Canvas is left to its terrible destiny, with nothing more than a couple of lines and circles.. isn’t it sad?
So, after this melodramatic introduction, we’re ready for today’s code: a fully featured, customizable, Canvas based calendar!
If you prefer a live demonstration rather than a simple screenshot, just go here: Canvas Calendar in action.
So, how to use it?
- Download its source code (CalendarWidget.java) and put it straight in your project
- Instantiate it within your Canvas with its plain-old-unique constructor:
CalendarWidget calendar = new CalendarWidget(new Date());
- Customize it with the colors/fonts/padding you prefer:
calendar.headerFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE); calendar.weekdayFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_MEDIUM); calendar.weekdayBgColor = 0xccccff; calendar.weekdayColor = 0x0000ff; calendar.headerColor = 0xffffff;
- After you’ve customized it, remember to always call its initialize() method:
calendar.initialize();
- Now, to paint it, you can simply call its paint() method from your Canvas paint(), like this:
protected void paint(Graphics g) { g.setColor(0xffffff); g.fillRect(0, 0, getWidth(), getHeight()); calendar.paint(g); }
- Now you must allow users to interact with it, so you can, for example, use Canvas keyPressed() method to interact with calendar:
protected void keyPressed(int key) { int keyCode = getGameAction(key); if(keyCode == FIRE) { Display.getDisplay(midlet).setCurrent( new Alert("Selected date", calendar.getSelectedDate().toString(), null, AlertType.CONFIRMATION) ); } else { calendar.keyPressed(keyCode); repaint(); } }
As you see, what we do is this:
- if the user press FIRE button, we alert the current selected date
- otherwise we call calendar keyPressed() method, to make it behave accordingly
- Other customizable properties include:
- MONTH_LABELS: change this to customize month labels in your own language
- WEEKDAY_LABELS: as above, change this to customize weekday labels
- startWeekday: this represents the week starting day, and its values range goes from 0 (for Monday) to 6 (for Sunday)
You can download source code of the example described above here: CalendarCanvas.java.
To get some more details about CalendarWidget source code, you can take a look at my article on Forum Nokia Wiki: Building a J2ME Canvas based Calendar / Date picker.
rachelwiz 1:19 pm on September 23, 2009 Permalink
Hi,
I created a midlet program and tried to compile this Calendar picker application, it gave me the following errors –
C:\WTK23\apps\CalendarMidlet\src\CalendarWidget.java:79: cannot find symbol
symbol : method ceil(double)
location: class java.lang.Math
this.weeks = (int)Math.ceil(((double)getStartWeekday() + getMonthDays()) / 7);
^
C:\WTK23\apps\CalendarMidlet\src\CalendarWidget.java:79: inconvertible types
found : java.lang.Math.ceil
required: int
this.weeks = (int)Math.ceil(((double)getStartWeekday() + getMonthDays()) / 7);
^
2 errors
com.sun.kvem.ktools.ExecutionException
Build failed
I also added “import java.lang.Math;” this line in my code but still its throwing the same error.
what may be the problem? Can you please help me in this.
Manolo 7:47 am on January 15, 2010 Permalink
Can i do that on Android, anyone can share some codes.
raj 8:28 am on October 14, 2010 Permalink
than q. this is very use full
panthibo 9:05 pm on April 24, 2011 Permalink
If you have errors with Math.ceil, try this:
replace
this.weeks = (int)Math.ceil(((double)getStartWeekday() + getMonthDays()) / 7);
by:
this.weeks = (int)(0.9 + ((double)getStartWeekday() + getMonthDays()) / 7);
It’s working
Rajesh 2:40 pm on May 24, 2011 Permalink
Hi
The Above Appllication runs fine in midlet but when we creat a JAD File it throws Exception
Try This using JAD File