Java Calendar Component HowTo

Below are some java code snippets which displays various features of the Java Calendar package.

  1. How to use the CalendarComboBox class?
  2. How to use the CalendarComponent class?
  3. How to use the CalendarDialog class?
  4. How to customize the colors, fonts and images?
  5. How to apply a style on the Calendar Skin?
  6. How to define a custom skin style?
  7. How to use an external skin and how to export a skin?
  8. How to customize the cells rendering?
  9. How to define a custom calendar renderer provider?
  10. How to show/hide the week?
  11. How to enable/disable the hover effect?
  12. How to change the first day of week?
  13. How to change the maximum number of letters used for displaying the week days names?
  14. How to change the maximum number of letters used for displaying the months names?
  15. How to be automatically informed when the date selected in the CalendarComponent changes?
  16. How to be automatically informed when some of the calendar properties are changed?
  17. How to change the locale used by the calendar component?
    
    
  1. How to use the CalendarComboBox class The code below shows how to use a CalendarComboBox class:
    CalendarFactory calendarFactory = CalendarFactory.getInstance();
    CalendarComboBox calendarComboBox = calendarFactory.createCalendarComboBox(new Date());
    
    CalendarComponent calendarComponent = calendarComboBox.getCalendarComponent();
    calendarComponent.setCalendarProperty(CalendarComponent.FIRST_DAY_OF_WEEK, new Integer(java.util.Calendar.MONDAY));
    calendarComponent.getCalendarSkin().applyCalendarSkinStyle(new CalendarSkinStyleAqua());
    calendarComponent.setCalendarRendererProvider(new CalendarRendererProviderRoundRect());
    
    JFrame frame = new JFrame("Calendar Test");
    JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout());
    panel.add(new JLabel("Enter a date:"));
    panel.add(calendarComboBox.getComboBoxComponent());
    frame.getContentPane().add(panel);
    frame.pack();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent arg0) {
        System.exit(0);
      }
    });
    frame.setVisible(true);
    
  2. How to use the CalendarComponent class The code below displays a simple usage of the CalendarComponent:
    CalendarFactory calendarFactory = CalendarFactory.getInstance();
    CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date());
    
    JFrame frame = new JFrame("Calendar Test");
    frame.getContentPane().add(calendarComponent.getComponent());
    frame.pack();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent arg0) {
        System.exit(0);
      }
    });
    frame.setVisible(true);
    
  3. How to use the CalendarDialog class The code below shows how to use a CalendarDialog class:
    CalendarDialog calendarDialog = CalendarFactory.getInstance().createCalendarDialog(null, new Date());
    CalendarComponent calendarComponent = calendarDialog.getCalendarComponent();
    calendarComponent.setCalendarProperty(CalendarComponent.FIRST_DAY_OF_WEEK, new Integer(java.util.Calendar.MONDAY));
    calendarComponent.getCalendarSkin().applyCalendarSkinStyle(new CalendarSkinStyleAqua());
    calendarComponent.setCalendarRendererProvider(new CalendarRendererProviderRoundRect());
    calendarDialog.getDialog().setTitle("Choose the date of the event");
    calendarDialog.getDialog().pack();
    calendarDialog.setVisible(true);
    Date date = calendarDialog.getDate();
    
  4. How to customize the colors, fonts and images All the colors, fonts and images informations are available in the CalendarSkin object. The code below modifies the text color in the calendar cells:
    CalendarFactory calendarFactory = CalendarFactory.getInstance();
    CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date());
    calendarComponent.getCalendarSkin().setSkinProperty(CalendarSkin.CELL_TEXT_COLOR, new Color(18, 230, 100));
    
    Below are other available calendar skin properties from the CalendarSkin object: - CELL_TEXT_COLOR - CELL_FILL_COLOR - SELECTED_CELL_TEXT_COLOR - SELECTED_CELL_FILL_COLOR - EXTRA_MONTH_CELL_TEXT_COLOR - WEEKEND_CELL_TEXT_COLOR - TODAY_COLOR - GRID_COLOR - WEEK_CELL_TEXT_COLOR - WEEK_CELL_FILL_COLOR - HEADER_CELL_TEXT_COLOR - HEADER_CELL_FILL_COLOR - HOVERED_CELL_TEXT_COLOR - HOVERED_CELL_FILL_COLOR - CELL_FONT - WEEKEND_CELL_FONT - EXTRA_MONTH_CELL_FONT - WEEK_CELL_FONT - HEADER_CELL_FONT - BACKGROUND_IMAGE - CELL_BACKGROUND_IMAGE
  5. How to apply a style on the Calendar Skin The code below shows how to apply a style on the Calendar Skin:
    CalendarFactory calendarFactory = CalendarFactory.getInstance();
    CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date());
    calendarComponent.getCalendarSkin().applyCalendarSkinStyle(new CalendarSkinStyleAqua());
    
    The skins are available in the java package com.imagine.component.calendar.skins. Also it can be used external skins. Please see API documentation for more details.
  6. How to define a custom skin style The user can also define very easy a skin style:
    class CalendarSkinStyleCustom extends CalendarSkinStyle {
    
      public void applyStyle(CalendarSkin calendarSkin) {
        calendarSkin.setSkinProperty(CalendarSkin.CELL_FONT, calendarSkin.getCalendarComponent().getComponent().getFont());
        calendarSkin.setSkinProperty(CalendarSkin.EXTRA_MONTH_CELL_FONT, calendarSkin.getCalendarComponent().getComponent().getFont().deriveFont(Font.ITALIC));
        calendarSkin.setSkinProperty(CalendarSkin.WEEKEND_CELL_FONT, calendarSkin.getCalendarComponent().getComponent().getFont().deriveFont(Font.ITALIC | Font.BOLD));
        calendarSkin.setSkinProperty(CalendarSkin.HEADER_CELL_FONT, calendarSkin.getCalendarComponent().getComponent().getFont());
        calendarSkin.setSkinProperty(CalendarSkin.WEEK_CELL_FONT, calendarSkin.getCalendarComponent().getComponent().getFont());
        
        calendarSkin.setSkinProperty(CalendarSkin.CELL_FILL_COLOR, new Color(189, 209, 173));
        calendarSkin.setSkinProperty(CalendarSkin.EXTRA_MONTH_CELL_TEXT_COLOR, new Color(128, 128, 128));
        calendarSkin.setSkinProperty(CalendarSkin.WEEKEND_CELL_TEXT_COLOR, new Color(54, 54, 16));
        calendarSkin.setSkinProperty(CalendarSkin.CELL_TEXT_COLOR, new Color(101, 101, 31));
        calendarSkin.setSkinProperty(CalendarSkin.TODAY_COLOR, new Color(255, 0, 0));
        
        calendarSkin.setSkinProperty(CalendarSkin.GRID_COLOR, new Color(134, 151, 130));
        
        calendarSkin.setSkinProperty(CalendarSkin.SELECTED_CELL_FILL_COLOR, new Color(82, 107, 61));
        calendarSkin.setSkinProperty(CalendarSkin.SELECTED_CELL_TEXT_COLOR, new Color(255, 255, 255));
        
        calendarSkin.setSkinProperty(CalendarSkin.HOVERED_CELL_FILL_COLOR, new Color(82, 107, 61));
        calendarSkin.setSkinProperty(CalendarSkin.HOVERED_CELL_TEXT_COLOR, new Color(255, 255, 255));
        
        calendarSkin.setSkinProperty(CalendarSkin.HEADER_CELL_TEXT_COLOR, new Color(212, 208, 200));
        calendarSkin.setSkinProperty(CalendarSkin.HEADER_CELL_FILL_COLOR, new Color(128, 128, 128));
        
        calendarSkin.setSkinProperty(CalendarSkin.WEEK_CELL_TEXT_COLOR, new Color(212, 208, 200));
        calendarSkin.setSkinProperty(CalendarSkin.WEEK_CELL_FILL_COLOR, new Color(128, 128, 128));
        
        
        calendarSkin.setSkinProperty(CalendarSkin.BACKGROUND_IMAGE, null);
        calendarSkin.setSkinProperty(CalendarSkin.CELL_BACKGROUND_IMAGE, null);
      }
      
      public String getName() {
        return "Custom";
      }
    }
      
    
    CalendarFactory calendarFactory = CalendarFactory.getInstance();
    CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date());
    calendarComponent.getCalendarSkin().applyCalendarSkinStyle(new CalendarSkinStyleCustom());
    
  7. How to use an external skin The code below shows how to use an external skin:
    CalendarFactory calendarFactory = CalendarFactory.getInstance();
    CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date());
    calendarComponent.getCalendarSkin().applyCalendarSkinStyle(new CalendarSkinStyleExternalFile(new File("./skins/skin01.skin")));
    
    calendarComponent.getCalendarSkin().applyCalendarSkinStyle(new CalendarSkinStyleExternalURL(new URL("http://www.java-calendar.com/skins/test.skin")));
    
    CalendarSkinStyleExternalFile.exportSkin(calendarComponent.getCalendarSkin(), new File("./skins/test.skin"));
    
  8. How to customize the cells rendering All the rendering is performed using the CalendarRendererProvider object. The code below changes from a calendar renderer provider to another:
    CalendarFactory calendarFactory = CalendarFactory.getInstance();
    CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date());
    calendarComponent.setCalendarRendererProvider(new CalendarRendererProviderRoundRect());
    
    The calendar renderers are available in the java package com.imagine.component.calendar.renderers. Please see API documentation for more details.
  9. How to define a custom calendar renderer provider The code below defines a custom calendar renderer provider which handle the drawing for some days differently from normal drawing:
    class CalendarTableCellRendererCustom implements CalendarTableCellRenderer {
    
      public void paint(Graphics g, CalendarComponent calendarComponent, CalendarTableCellInfo calendarTableCellInfo) {
      	// If the calendar has a background renderer then this will actually paint the calendar background. 
      	// So this table cell renderer will no longer paint the cell background in this case. 
        if (!calendarComponent.getCalendarRendererProvider().hasBackgroundRenderer(calendarComponent)) {
          g.setColor(calendarComponent.getCalendarSkin().getColorSkinProperty(CalendarSkin.CELL_FILL_COLOR));
          g.fillRect(0, 0, calendarTableCellInfo.getWidth(), calendarTableCellInfo.getHeight());
          
          Image cellBackGroundImage = calendarComponent.getCalendarSkin().getImageSkinProperty(CalendarSkin.CELL_BACKGROUND_IMAGE);
          if (cellBackGroundImage != null) {
            g.drawImage(cellBackGroundImage, 0, 0, null);
          }
        }
        
        g.setColor(new Color(255, 0, 0));
        g.fillOval(0, 0, calendarTableCellInfo.getWidth(), calendarTableCellInfo.getHeight());
        
        g.setColor(new Color(0, 0, 255));
        drawCenteredString(g, calendarTableCellInfo.getWidth(), calendarTableCellInfo.getHeight(), (String)calendarTableCellInfo.getValue());
      }
    
      public String toString() {
        return "Custom";
      }
      
      public static void drawCenteredString(Graphics g, int width, int height, String text) {
        FontMetrics fontMetrics = g.getFontMetrics();
        int currentWidth = fontMetrics.stringWidth(text);
        int currentHeight = fontMetrics.getAscent() - 3;
        
        int x = (width - currentWidth) / 2;
        if (x < 0) {
          x = 0;
        }
        int y = (height - currentHeight) / 2;
        if (y < 0) {
          y = 0;
        }
        y = height - y;
        
        g.drawString(text, x, y);
      }
    }  
    
    class CalendarRendererProviderCustom extends CalendarRendererProviderDefault {
    
      CalendarTableCellRendererCustom calendarTableCellRendererCustom = new CalendarTableCellRendererCustom();
    
      public CalendarTableCellRenderer getTableCellRenderer(CalendarComponent calendarComponent, CalendarTableCellInfo calendarTableCellInfo) {
        if (calendarTableCellInfo.getMonthType() == CalendarMonthType.CURRENT_MONTH && calendarTableCellInfo.getDayOfMonth() == 12 || calendarTableCellInfo.getDayOfMonth() == 21) {
          return calendarTableCellRendererCustom;
        }
        
        return super.getTableCellRenderer(calendarComponent, calendarTableCellInfo);
      }
      
      
      public String getName() {
        return "Custom";
      }
    }
    
    CalendarFactory calendarFactory = CalendarFactory.getInstance();
    CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date());
    calendarComponent.setCalendarRendererProvider(new CalendarRendererProviderCustom());
    
  10. How to change the first day of week The code below changes the first day of week to Tuesday:
    CalendarFactory calendarFactory = CalendarFactory.getInstance();
    CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date());
    calendarComponent.setCalendarProperty(CalendarComponent.FIRST_DAY_OF_WEEK, new Integer(java.util.Calendar.TUESDAY));
    
  11. How to show/hide the week The code below shows how to show/hide the week:
    CalendarFactory calendarFactory = CalendarFactory.getInstance();
    CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date());
    //    show week
    calendarComponent.setCalendarProperty(CalendarComponent.SHOW_WEEK, new Boolean(true));
    //    hide week
    calendarComponent.setCalendarProperty(CalendarComponent.SHOW_WEEK, new Boolean(false));
    
  12. How to enable/disable the hover effect The code below shows how to enable/disable the hover effect:
    CalendarFactory calendarFactory = CalendarFactory.getInstance();
    CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date());
    //enable hover effect
    calendarComponent.setCalendarProperty(CalendarComponent.SHOW_HOVER, new Boolean(true));
    //disable hover effect
    calendarComponent.setCalendarProperty(CalendarComponent.SHOW_HOVER, new Boolean(false));
    
  13. How to change the maximum number of letters used for displaying the week days names The code below changes the maximum number of letters used for displaying the week days names:
    CalendarFactory calendarFactory = CalendarFactory.getInstance();
    CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date());
    calendarComponent.setCalendarProperty(CalendarComponent.MAX_WEEK_DAYS_NAMES_LENGTH, new Integer(2));
    
  14. How to change the maximum number of letters used for displaying the months names The code below changes the maximum number of letters used for displaying the months names:
    CalendarFactory calendarFactory = CalendarFactory.getInstance();
    CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date());
    calendarComponent.setCalendarProperty(CalendarComponent.MAX_MONTHS_NAMES_LENGTH, new Integer(5));
    
  15. How to be automatically informed when the date selected in the CalendarComponent changes The code below shows how to add a date listener:
    CalendarFactory calendarFactory = CalendarFactory.getInstance();
    CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date());
    calendarComponent.addDateListener(new DateListener() {
      public void dateChanged(Date date) {
        // some java code here
      }
    });
    
  16. How to be automatically informed when some of the calendar properties are changed The code below shows how create a CalendarComponentListener:
    CalendarFactory calendarFactory = CalendarFactory.getInstance();
    CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date());
    calendarComponent.addCalendarComponentListener(new CalendarComponentListener() {
      public void processCalendarEvent(CalendarEvent calendarEvent) {
        if (calendarEvent instanceof CalendarEventFirstDayOfWeekChanged) {
          CalendarEventFirstDayOfWeekChanged calendarEventFirstDayOfWeekChanged = (CalendarEventFirstDayOfWeekChanged)calendarEvent;
          System.out.println("First day of week:" + calendarEventFirstDayOfWeekChanged.getFirstDayOfWeek());
          // some java code here
        } else if (calendarEvent instanceof CalendarEventMaxMonthsNamesLengthChanged) {
          CalendarEventMaxMonthsNamesLengthChanged calendarEventMaxMonthsNamesLengthChanged = (CalendarEventMaxMonthsNamesLengthChanged)calendarEvent;
          System.out.println("Max Months Names Length:" + calendarEventMaxMonthsNamesLengthChanged.getMaxMonthsNamesLength());
          // other java code here
        }
      }
    
      public Class[] getEventsListened() {
        return new Class[] {
            CalendarEventFirstDayOfWeekChanged.class,
            CalendarEventMaxMonthsNamesLengthChanged.class
        };
      }
    });
    
  17. How to change the locale used by the calendar component The code below shows how to change the calendar locale:
    CalendarFactory calendarFactory = CalendarFactory.getInstance();
    CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date());
    calendarComponent.setCalendarProperty(CalendarComponent.LOCALE, Locale.FRANCE);