{"id":2296,"date":"2020-06-01T13:09:57","date_gmt":"2020-06-01T13:09:57","guid":{"rendered":"https:\/\/mindfusion.eu\/blog\/?p=2296"},"modified":"2021-01-25T16:13:47","modified_gmt":"2021-01-25T16:13:47","slug":"a-monthly-calendar-in-java-swing-that-ends-at-a-given-date","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/a-monthly-calendar-in-java-swing-that-ends-at-a-given-date\/","title":{"rendered":"A Monthly Calendar in Java Swing that Ends at a Given Date"},"content":{"rendered":"<p>In this blog post we will build a monthly calendar in Java Swing using the scheduler library. We will use the <a title=\"MindFusion Scheduling Library for Java Swing: CalendarView\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_CalendarView_0.htm\">monthly view<\/a> of the calendar but we will make it render only 3 months after the current month. By default there are no limits how far users can scroll the months in Single month view both back- and forth-wards. We will let our users scroll as many months they want in the past but only scroll 3 months ahead.<\/p>\n<p><img decoding=\"async\" title=\"A Time-restricted Monthly Calendar in Java Swing\" src=\"https:\/\/mindfusion.dev\/samples\/java\/scheduler\/time_restricted_monthly_calendar.png\" \/><\/p>\n<p><!--more--><\/p>\n<p><strong>I. General Settings<\/strong><\/p>\n<p>We create an empty project in Eclipse and add the JPlanner.jar as an external Jar library as shows in this picture:<\/p>\n<p><img decoding=\"async\" title=\"Adding the Java Scheduler Jar File\" src=\"https:\/\/mindfusion.dev\/samples\/java\/scheduler\/jplanner_jar1.png\" \/><\/p>\n<p>Then we create a Java class that extends JFrame and there, in the constructor we create a new instance of the <a title=\"MindFusion Scheduling Library for Java Swing: Calendar\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_awt_AwtCalendar.htm\">Calendar<\/a> class:<\/p>\n<pre>calendar = new Calendar();\ngetContentPane().add(calendar, BorderLayout.CENTER);<\/pre>\n<p>Then we set the current view to be <a title=\"MindFusion Scheduling Library for Java Swing: SingleMonth\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?VistaSingleMonth.png\">SingleMonth<\/a> using the <a title=\"MindFusion Scheduling Library for Java Swing: setCurrentView\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?M_com_mindfusion_scheduling_awt_AwtCalendar_setCurrentView_1_Int32.htm\">setCurrentView<\/a> method and we set the theme to be silver with <a title=\"MindFusion Scheduling Library for Java Swing: setTheme\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?M_com_mindfusion_scheduling_awt_AwtCalendar_setTheme_1_Int32.htm\">setTheme<\/a> calendar supports a variety ot views and themes, which are members of the <a title=\"MindFusion Scheduling Library for Java Swing: CalendarView\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_CalendarView_0.htm\">CalendarView<\/a> and <a title=\"MindFusion Scheduling Library for Java Swing: ThemeType\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_ThemeType_0.htm\">ThemeType<\/a> enumerations.<\/p>\n<p><strong>II. Handling Events<\/strong><\/p>\n<p>We will use the <a title=\"MindFusion Scheduling Library for Java Swing: addCalendarListener\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?M_com_mindfusion_scheduling_awt_AwtCalendar_addCalendarListener_1_CalendarListener.htm\">addCalendarListener<\/a> method to add an instance of the CalendarAdapter class that is used to handle events in the <a title=\"MindFusion Scheduling Library for Java Swing: Calendar\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_awt_AwtCalendar.htm\">Calendar<\/a><\/p>\n<pre>calendar.addCalendarListener(new CalendarAdapter(){\t\t\n\t\t\n\t\t@Override()\n\t\tpublic void visibleDateChanged(DateChangedEvent e) {\n\t\t\tonVisibleDateChanged(e);\n\t\t}\n\t\t\n\t});<\/pre>\n<p>We will handle the <a title=\"MindFusion Scheduling Library for Java Swing: visibleDateChanged\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?M_com_mindfusion_scheduling_CalendarAdapter_visibleDateChanged_1_DateChangedEvent.htm\">visibleDateChanged<\/a> event and check when the user is about to scroll to a month that we do not want to show. In our sample we want the user to be able to scroll only three months in advance.<\/p>\n<p>The <a title=\"MindFusion Scheduling Library for Java Swing: Calendar\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_awt_AwtCalendar.htm\">Calendar<\/a> initializes by default with the current date being visible. For a <a title=\"MindFusion Scheduling Library for Java Swing: CalendarView\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_CalendarView_0.htm\">CalendarView<\/a> this means the current month is rendered. We will keep this date in a global variable for the class because we want to be able to use it in the event handler method:<\/p>\n<pre>protected MainWindow()\n{\n\tsetDefaultCloseOperation(EXIT_ON_CLOSE);\n\tsetSize(400, 400);\n\tsetTitle(\"Tutorial 1\");\n\tinitialDate = DateTime.now();\n\t...........................\n\t...........................\n}\n\nprivate Calendar calendar;\nprivate DateTime initialDate;<\/pre>\n<p>We will use our initialDate variable to reset the calendar to a data three months after it. Whenever we detect that the user is about to scroll to the 4th month, we reset the date to be 3 months after initialDate&#8217;s month. Here is how:<\/p>\n<pre>\/\/make sure that dates are rendered till the end of May\npublic void onVisibleDateChanged(DateChangedEvent e)\n{\n\t\t\t\t\t\n\tif(e.getNewDate().getMonth() == initialDate.getMonth() + 4)\n\t{\t\t\n\t\tcalendar.setDate(new DateTime(initialDate.getYear(), \n\t\t\t\tinitialDate.getMonth() + 3, initialDate.getDay()));\n\t}\n}<\/pre>\n<p>Now if the user want to go to the 4th month, the view will always bring the 3rd month and will not allow switiching to the month ahead.<\/p>\n<p>With that our tutorial is finished. You can download the sample code from this link:<\/p>\n<p align=\"center\"><a title=\"Monthly Calendar with Time Restrictions\" href=\"https:\/\/mindfusion.dev\/samples\/java\/scheduler\/timeRestrictedCalendar.zip\">Monthly Calendar with Fixed End Date<\/a><\/p>\n<p>Technical support is available at the <a title=\"Java Swing Discussion Board\" href=\"https:\/\/mindfusion.dev\/Forum\/YaBB.pl?board=scheduling_swing\">Java Swing Online Discussion Forum<\/a>.<\/p>\n<p><em>About MindFusion Scheduling for Java Swing:<\/em> The library provides extensive feature-set for creating and customizing all sorts of calendars, task lists, time-management tables, resource allocation tables and other. It boasts various options for customizing appearance and numerous events for handling user actions. The distribution archive includes a lot of samples and detailed documentation. Learn more at <a href=\"https:\/\/mindfusion.dev\/java-scheduler.html\">https:\/\/mindfusion.dev\/java-scheduler.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog post we will build a monthly calendar in Java Swing using the scheduler library. We will use the monthly view of the calendar but we will make it render only 3 months after the current month. By &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/a-monthly-calendar-in-java-swing-that-ends-at-a-given-date\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[535,104],"tags":[651,650,649,652],"class_list":["post-2296","post","type-post","status-publish","format-standard","hentry","category-java-swing","category-scheduling-2","tag-java-code","tag-java-library","tag-java-swing-scheduler","tag-time-restricted-calendar"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-B2","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/comments?post=2296"}],"version-history":[{"count":4,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2296\/revisions"}],"predecessor-version":[{"id":2658,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2296\/revisions\/2658"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=2296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=2296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=2296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}