A Monthly Calendar in Java Swing that Ends at a Given Date

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 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.

I. General Settings

We create an empty project in Eclipse and add the JPlanner.jar as an external Jar library as shows in this picture:

Then we create a Java class that extends JFrame and there, in the constructor we create a new instance of the Calendar class:

calendar = new Calendar();
getContentPane().add(calendar, BorderLayout.CENTER);

Then we set the current view to be SingleMonth using the setCurrentView method and we set the theme to be silver with setTheme calendar supports a variety ot views and themes, which are members of the CalendarView and ThemeType enumerations.

II. Handling Events

We will use the addCalendarListener method to add an instance of the CalendarAdapter class that is used to handle events in the Calendar

calendar.addCalendarListener(new CalendarAdapter(){		
		
		@Override()
		public void visibleDateChanged(DateChangedEvent e) {
			onVisibleDateChanged(e);
		}
		
	});

We will handle the visibleDateChanged 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.

The Calendar initializes by default with the current date being visible. For a CalendarView 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:

protected MainWindow()
{
	setDefaultCloseOperation(EXIT_ON_CLOSE);
	setSize(400, 400);
	setTitle("Tutorial 1");
	initialDate = DateTime.now();
	...........................
	...........................
}

private Calendar calendar;
private DateTime initialDate;

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’s month. Here is how:

//make sure that dates are rendered till the end of May
public void onVisibleDateChanged(DateChangedEvent e)
{
					
	if(e.getNewDate().getMonth() == initialDate.getMonth() + 4)
	{		
		calendar.setDate(new DateTime(initialDate.getYear(), 
				initialDate.getMonth() + 3, initialDate.getDay()));
	}
}

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.

With that our tutorial is finished. You can download the sample code from this link:

Monthly Calendar with Fixed End Date

Technical support is available at the Java Swing Online Discussion Forum.

About MindFusion Scheduling for Java Swing: 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 https://mindfusion.eu/java-scheduler.html