JComponentPack Programmer Guide > JCalendar

JCalendar - Java Date Picker

JCalendar provides a Java calendar component, a Java date picker.

Header Style

JCalendar provides three header styles: class arrow, modern arrow, none.

Code Samples:

// create JCalendar component
JCalendar calendar = new JCalendar();
// classic arrow
calendar.putClientProperty("JCalendar.headerStyle", "Classic_Arrow");
// modern arrow
calendar.putClientProperty("JCalendar.headerStyle", "Modern_Arrow");
// none
calendar.putClientProperty("JCalendar.headerStyle", "None");

Center Style

JCalendar provides two center styles: month view, year view.

Code Samples:

// create JCalendar component
JCalendar calendar = new JCalendar();
// month view
calendar.putClientProperty("JCalendar.centerStyle", "MonthView");
// year view
calendar.putClientProperty("JCalendar.centerStyle", "YearView");

Footer Style

JCalendar provides two footer styles: today, none.

Code Samples:

// create JCalendar component
JCalendar calendar = new JCalendar();
// show today button
calendar.putClientProperty("JCalendar.footerStyle", "Today");
// hide the today button
calendar.putClientProperty("JCalendar.footerStyle", "None");

Highlight Date

JCalendar supports highlight date, you can change the date background and foreground. The following code will change the date 1, 3, 5, 7, 23 foreground and background, sets the foreground to red, and background to yellow.

Code Samples:

// a sample AttributesProvider
    private static class DefaultAttributesProvider implements AttributesProvider {
	private SimpleAttributeSet a;
	public DefaultAttributesProvider() {
	    a = new SimpleAttributeSet();
	    StyleConstants.setForeground(a, Color.red);
	    StyleConstants.setBackground(a, Color.yellow);
	}
	public AttributeSet getAttributes(Object value) {
	    if(value != null) {
		int day = ((Integer)value).intValue();
		switch(day) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 23:
		    return a;
		}
	    }
	    return null;
	}
    }
// attach it to JCalendar component
JCalendar calendar = new JCalendar();
calendar.setCellProvider(new DefaultAttributesProvider());    

Change Date Font

JCalendar component also supports the different date fonts. The following code will change the date 1, 3, 5, 7, 23 font, sets the font family to "Dialog", font size to 20 and font style to bold.

Code Samples:

// a sample AttributesProvider
    private static class DefaultAttributesProvider implements AttributesProvider {
	private SimpleAttributeSet a;
	public DefaultAttributesProvider() {
	    a = new SimpleAttributeSet();
	    StyleConstants.setFontFamily(a, "Dialog");
	    StyleConstants.setFontSize(a, 20);
	    StyleConstants.setBold(a, true);
	}
	public AttributeSet getAttributes(Object value) {
	    if(value != null) {
		int day = ((Integer)value).intValue();
		switch(day) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 23:
		    return a;
		}
	    }
	    return null;
	}
    }
// attach it to JCalendar component
JCalendar calendar = new JCalendar();
calendar.setCellProvider(new DefaultAttributesProvider());    

Support Action

When double click or the "ENTER" key on the keyboard has been pressed, the JCalendar component will generate an action event.

Code Samples:
// add the action listener
calendar.addActionListener(new MyActionListener());
// set the action click count to 1
calendar.setActionClickCount(2);
// enable the today button action
calendar.putClientProperty("JCalendar.todayButtonFireActionEventEnabled", Boolean.TRUE);

JCalendar properties

You can customize the selection date format and selection label, you can also specify the selection time style.

Code Samples:
// change the selection date format
calendar.setSelectionDateFormat(myDateFormat);
// hide the selection label
calendar.setSelectionLabelVisible(false);
// change the selection time style
// the selection date will adjust to start of the day, it's hours, minutes,
// seconds field will change to 0
calendar.setAdjustTimeStyle(JCalendar.AdjustTimeStyle.START_OF_DAY);

Customize Cell Renderer and Provider

You can customize the JCalendar's cell renderer and cell provider for your requirements.

Code Samples:
// set my cell renderer
calendar.setCellRenderer(new MyCellRenderer());
// set my cell provider
calendar.setCellProvider(new MyCellProvider());

Related JavaDoc API List

JCalendar
JCalendar.AdjustTimeStyle
AbstractCellRenderer
CellProvider


Copyright (C) 2009 Extreme Component, Inc. All rights reserved.