Below are some java code snippets which displays various features of the Java Calendar package.
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);
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);
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();
CalendarFactory calendarFactory = CalendarFactory.getInstance(); CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date()); calendarComponent.getCalendarSkin().setSkinProperty(CalendarSkin.CELL_TEXT_COLOR, new Color(18, 230, 100));
CalendarFactory calendarFactory = CalendarFactory.getInstance(); CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date()); calendarComponent.getCalendarSkin().applyCalendarSkinStyle(new CalendarSkinStyleAqua());
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.DISABLED_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); calendarSkin.setSkinProperty(CalendarSkin.STATUS_PANEL_TEXT_COLOR, new Color(101, 101, 31)); calendarSkin.setSkinProperty(CalendarSkin.STATUS_PANEL_HOVERED_TEXT_COLOR, new Color(82, 107, 61)); calendarSkin.setSkinProperty(CalendarSkin.STATUS_PANEL_FILL_COLOR, new Color(189, 209, 173)); calendarSkin.setSkinProperty(CalendarSkin.STATUS_PANEL_FONT, calendarSkin.getCalendarComponent().getComponent().getFont().deriveFont(Font.BOLD)); } public String getName() { return "Custom"; } } CalendarFactory calendarFactory = CalendarFactory.getInstance(); CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date()); calendarComponent.getCalendarSkin().applyCalendarSkinStyle(new CalendarSkinStyleCustom());
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"));
CalendarFactory calendarFactory = CalendarFactory.getInstance(); CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date()); calendarComponent.setCalendarRendererProvider(new CalendarRendererProviderRoundRect());
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()) { 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()); if (calendarTableCellInfo.isDisabled()) { g.setColor(calendarComponent.getCalendarSkin().getColorSkinProperty(CalendarSkin.DISABLED_COLOR)); int count = 3; g.drawLine(count, count, calendarTableCellInfo.getWidth() - count, calendarTableCellInfo.getHeight() - count); g.drawLine(count, calendarTableCellInfo.getHeight() - count, calendarTableCellInfo.getWidth() - count, count); } } 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(CalendarTableCellInfo calendarTableCellInfo) { if (calendarTableCellInfo.getMonthType() == CalendarMonthType.CURRENT_MONTH && calendarTableCellInfo.getDayOfMonth() == 12 || calendarTableCellInfo.getDayOfMonth() == 21) { return calendarTableCellRendererCustom; } return super.getTableCellRenderer(calendarTableCellInfo); } public String getName() { return "Custom"; } } CalendarFactory calendarFactory = CalendarFactory.getInstance(); CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date()); calendarComponent.setCalendarRendererProvider(new CalendarRendererProviderCustom());
CalendarFactory calendarFactory = CalendarFactory.getInstance(); CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date()); calendarComponent.setCalendarProperty(CalendarComponent.FIRST_DAY_OF_WEEK, new Integer(java.util.Calendar.TUESDAY));
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));
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));
CalendarFactory calendarFactory = CalendarFactory.getInstance(); CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date()); calendarComponent.setCalendarProperty(CalendarComponent.MAX_WEEK_DAYS_NAMES_LENGTH, new Integer(2));
CalendarFactory calendarFactory = CalendarFactory.getInstance(); CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date()); calendarComponent.setCalendarProperty(CalendarComponent.MAX_MONTHS_NAMES_LENGTH, new Integer(5));
CalendarFactory calendarFactory = CalendarFactory.getInstance(); CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date()); calendarComponent.addDateListener(new DateListener() { public void dateChanged(Date date) { // some java code here } });
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 }; } });
CalendarFactory calendarFactory = CalendarFactory.getInstance(); CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date()); calendarComponent.setCalendarProperty(CalendarComponent.LOCALE, Locale.FRANCE);
CalendarFactory calendarFactory = CalendarFactory.getInstance(); CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date()); calendarComponent.setCalendarSelectionModel(new CalendarSelectionModelSingleIntervalSelection());
class CalendarSelectionModelCustom extends CalendarSelectionModelMultipleIntervalSelection { private GregorianCalendar calendar = new GregorianCalendar(); public boolean isDateDisabled(Date date) { calendar.setTime(date); int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); return (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY); } public String getName() { return "Custom"; } } CalendarFactory calendarFactory = CalendarFactory.getInstance(); CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date()); calendarComponent.setCalendarSelectionModel(new CalendarSelectionModelCustom());
CalendarFactory calendarFactory = CalendarFactory.getInstance(); CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date()); calendarComponent.getCalendarSelectionModel().addCalendarSelectionListener(new CalendarSelectionListener() { public void selectionChanged(CalendarSelectionModel calendarSelectionModel) { Date[] selectedDates = calendarSelectionModel.getSelectedDates(); // Process the selected dates. } });
CalendarFactory calendarFactory = CalendarFactory.getInstance(); final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd"); JTable table = new JTable(new Object[][] {{"First Event", new Date()}, {"Second Event", new Date()}}, new Object[] {"Event Name", "Date"}); table.setDefaultEditor(Date.class, calendarFactory.createTableCellEditor()); table.setDefaultRenderer(Date.class, new DefaultTableCellRenderer() { protected void setValue(Object value) { if (value == null) { setText("null"); } else { setText(simpleDateFormat.format((Date) value)); } } });
CalendarFactory calendarFactory = CalendarFactory.getInstance(); CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date()); // show status panel calendarComponent.setCalendarProperty(CalendarComponent.SHOW_STATUS_PANEL, new Boolean(true)); // hide status panel calendarComponent.setCalendarProperty(CalendarComponent.SHOW_STATUS_PANEL, new Boolean(false));
CalendarFactory calendarFactory = CalendarFactory.getInstance(); CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date()); // enable null dates calendarComponent.getCalendarSelectionModel().setEmptySelectionAllowed(true); // disable null dates calendarComponent.getCalendarSelectionModel().setEmptySelectionAllowed(false);
CalendarFactory calendarFactory = CalendarFactory.getInstance(); CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date()); // set text for today label calendarComponent.setCalendarProperty(CalendarComponent.STATUS_PANEL_TODAY_LABEL, "Hoy: {MM.dd.yyyy}"); // set text for none label calendarComponent.setCalendarProperty(CalendarComponent.STATUS_PANEL_NONE_LABEL, "Ninguno");
CalendarFactory calendarFactory = CalendarFactory.getInstance(); final CalendarComponent calendarComponent = calendarFactory.createCalendarComponent(new Date()); calendarComponent.addCalendarComponentListener(new CalendarComponentListener() { public Class[] getEventsListened() { return new Class[]{CalendarEventLocaleChanged.class}; } public void processCalendarEvent(CalendarEvent calendarEvent) { Locale locale = calendarComponent.getLocaleCalendarProperty(CalendarComponent.LOCALE); if (locale.getLanguage().equals(Locale.ENGLISH.getLanguage())) { calendarComponent.setCalendarProperty(CalendarComponent.STATUS_PANEL_TODAY_LABEL, "Today: {MM.dd.yyyy}"); calendarComponent.setCalendarProperty(CalendarComponent.STATUS_PANEL_NONE_LABEL, "None"); } else if (locale.getLanguage().equals(Locale.GERMAN.getLanguage())) { calendarComponent.setCalendarProperty(CalendarComponent.STATUS_PANEL_TODAY_LABEL, "Heute: {MM.dd.yyyy}"); calendarComponent.setCalendarProperty(CalendarComponent.STATUS_PANEL_NONE_LABEL, "Kein"); } else if (locale.getLanguage().equals(Locale.FRENCH.getLanguage())) { calendarComponent.setCalendarProperty(CalendarComponent.STATUS_PANEL_TODAY_LABEL, "Aujourd''hui: {MM.dd.yyyy}"); calendarComponent.setCalendarProperty(CalendarComponent.STATUS_PANEL_NONE_LABEL, "Aucun"); } else if (locale.getLanguage().equals(Locale.ITALIAN.getLanguage())) { calendarComponent.setCalendarProperty(CalendarComponent.STATUS_PANEL_TODAY_LABEL, "Oggi: {MM.dd.yyyy}"); calendarComponent.setCalendarProperty(CalendarComponent.STATUS_PANEL_NONE_LABEL, "Nessuno"); } else if (locale.getLanguage().equals(new Locale("es", "", "").getLanguage())) { calendarComponent.setCalendarProperty(CalendarComponent.STATUS_PANEL_TODAY_LABEL, "Hoy: {MM.dd.yyyy}"); calendarComponent.setCalendarProperty(CalendarComponent.STATUS_PANEL_NONE_LABEL, "Ninguno"); } else if (locale.getLanguage().equals(new Locale("ro", "", "").getLanguage())) { calendarComponent.setCalendarProperty(CalendarComponent.STATUS_PANEL_TODAY_LABEL, "Astazi: {MM.dd.yyyy}"); calendarComponent.setCalendarProperty(CalendarComponent.STATUS_PANEL_NONE_LABEL, "Nimic"); } } });