Have you ever wondered why Matlab does not have standard GUI date-handling components?
Matlab has many built-in date-handling functions (calendar, date, datestr, datenum, datetick, datevec etc.). Unfortunately, this built-in support does not extend to Matlab GUI. If we need a date-selection drop-down or calendar panel we have to design it ourselves, or use a third-party Java component or ActiveX control.
JIDE Components
Luckily, we have a much better alternative, right within Matlab. This relies on the undocumented fact that Matlab uses JIDE components for many of its GUI components. As already explained earlier, JIDE controls are pre-bundled in Matlab (/java/jarext/jide/jide-grids.jar under the Matlab root). You can find further details on JIDE Grids in the Developer Guide (pages 28-35) and the Javadoc documentation.
In particular, JIDE Grids includes the following date-selection controls:
- DateChooserPanel – an extension of Swing’s JPanel that displays a single month and enables selecting one or more days
- CalendarViewer – a similar panel, that displays several months in a table-format (e.g., 4×3 months)
- DateComboBox – a combo-box (drop-down/popup menu) that presents a DateChooserPanel for selecting a date
- DateSpinnerComboBox – presents a date-selection combo-box that includes both the DateComboBox and a spinner control (this control is only available in the latest Matlab releases)
- MonthChooserPanel – a panel that enables selection of entire months (not specific dates)
- MonthComboBox – a month selection combo-box, similar to DateComboBox but without the ability to select individual days
Usage of these controls is very similar, so I’ll just show the basics here. First, to present any control, we need to use the built-in javacomponent function or the uicomponent utility:
% Initialize JIDE's usage within Matlab com.mathworks.mwswing.MJUtilities.initJIDE; % Display a DateChooserPanel jPanel = com.jidesoft.combobox.DateChooserPanel; [hPanel,hContainer] = javacomponent(jPanel,[10,10,200,200],gcf)

DateChooserPanel and MonthChooserPanel components

2x2 CalendarViewer component
Just as with any Java object, properties may either be accessed with the Java accessor methods (e.g. getName() or setName(name)), or the Matlab get/set semantics (e.g. get(prop,’Name’) or set(prop,’Name’,value)). When using the Matlab syntax, remember to wrap the Java object in a handle() call, to prevent a memory leak (or use hPanel rather than jPanel):
jPanel.setShowWeekNumbers(false); % Java syntax set(hPanel,'ShowTodayButton',true); % Matlab syntax
Retrieving the selected date is easy:
>> selectedDate = jPanel.getSelectedDate() selectedDate = Sun Jun 27 00:00:00 IDT 2010 % or: selectedDate = get(jPanel,'SelectedDate'); % Note: selectedDate is a java.util.Date object: >> selectedDate.get Class = [ (1 by 1) java.lang.Class array] Date = [27] Day = [0] Hours = [0] Minutes = [0] Month = [5] Seconds = [0] Time = [1.27759e+012] TimezoneOffset = [-180] Year = [110]
We can enable selection of multiple dates (SINGLE_SELECTION=0, SINGLE_INTERVAL_SELECTION=1,MULTIPLE_INTERVAL_SELECTION=2):
jModel = hPanel.getSelectionModel; % a com.jidesoft.combobox.DefaultDateSelectionModel object jModel.setSelectionMode(jModel.MULTIPLE_INTERVAL_SELECTION); >> hPanel.getSelectionModel.getSelectedDates ans = java.util.Date[]: [java.util.Date] [java.util.Date] [java.util.Date]
And of course we can set a callback for whenever the user modifies the selected date(s):
hModel = handle(hPanel.getSelectionModel, 'CallbackProperties'); set(hPanel, 'ValueChangedCallback', @myCallbackFunction);
For the combo-box (drop-down/popup menus) controls, we obviously need to modify the displayed size (in the javacomponent call) to something much more compact, such as [10,10,100,20]. These components display one of the above panels as their pop-up selection panels. Users can access and customize these panels using the combo-box control’s getPopupPanel() function (or PopupPanel property).

DateComboBox and DateSpinnerComboBox components
Numerous other customizations are possible with these JIDE components – have fun exploring (my uiinspect utility can be quite handy in this)! Just remember that JIDE evolves with Matlab, and so JIDE’s online documentation, which refers to the latest JIDE version, may be partially inapplicable if you use an old Matlab version. The older your Matlab, the more such inconsistencies that you may find.
Alternative components
There are several alternatives to the JIDE components:
If we have Matlab’s Financial toolbox, we can use the uicalendar function. Unfortunately, this control is not available if you don’t own the expensive Financial toolbox.
If we only target Windows-based platforms, we could use third-party ActiveXes such as the Microsoft Date-and-Time-Picker (MSComCtl2.DTPicker.2), Microsoft MonthView (MSComCtl2.MonthView.2) or the Microsoft Office Calendar (MSCAL.Calendar.7) ActiveX controls. Depending on your installed applications, you may have other similar controls. For example, if you have Symantec’s EndPoint Protection (SEP), you have access to the SEP Date Control (LDDATETIME.LDDateCtrl.1). Of course, these controls will not work on non-Windows platforms, or platforms that do not have these ActiveX controls installed.
We can also use other (non-JIDE) third-party Java controls from places like javashareware.com, swinglabs.org, downloadthat.com, sharewareconnection.com, easyfreeware.com, l2fprod.com, fileheap.com/software/components.html, swing-components.safe-install.com and many others. One specific example is NachoCalendar, from SourceForge.com.
Finally, we could use some of the utilities posted on the Matlab File Exchange: uical, uisetdate, calender (sic) and several others.
In my own biased opinion, none of these alternatives comes close to the ease-of-use and functionality of the JIDE components presented above. What do you think? Please add your comments here.
Related posts:
- Color selection components Matlab has several internal color-selection components that can easily be integrated in Matlab GUI...
- Plot-type selection components Several built-in components enable programmatic plot-type selection in Matlab GUI - this article explains how...
- Setting status-bar components Matlab status-bars are Java containers in which we can add GUI controls such as progress-bars, not just simple text labels...
- Figure toolbar components Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to access existing toolbar icons and how to add non-button toolbar components....
- Uitable customization report In last week’s report about uitable sorting, I offered a report that I have written which covers uitable customization in detail. Several people have asked for more details about the contents of this report. This is a reasonable request, and...
- Tri-state checkbox Matlab checkboxes can easily be made to support tri-state functionality....



Could you please explain how to change the date format of the DateSpinnerComboBox? I poked around with the findjobj tool, but it appears the format must be a java class?
Currently it reads 30/06/10. How would you change it to appear as June 30, 2010?
Thanks.
@Mike – you can use the java SimpleDateFormat class:
Thanks, Donn. I appreciate your help.
Ok, I’m starting to love them java components in my GUI, but I still dont know anything about java.
How would I initialize a date for the DateComboBox?
@Mike – you can use the setDate() function, as follows:
Use my UIInspect utility or Matlab’s built-in methodsview function to explore the dozens of available methods.
Thanks for your response and your patience. Java syntax is completely new to me, so I have great difficulty reading the doc pages online.
I inserted the OK button with
Now I want the window to close when I push the OK button.
How can I do that by using callback?
@gunnar – there is no easy answer I think. If you wish, I could develop a small function that shows you how to do this – email me.
Thank you for posting this!
I created a DateChooserPanel with a ValueChangedCallback assigned to it, but it is not triggered when i change the month. Is there a separate callback for this? uiinspect only shows ValueChangedCallback.
Is there any way to get a callback to trigger when changing the month?
@Henric – clicking the month is not an actionable event in
DateChooserPanel– only selecting a specific date is actionable. If you wish to select a month, useMonthChooserPanelinstead.Hi, Yair,
I’m trying to implement a callback for the selection from a DateSpinnerComboBox. Unfortunately, it does not appear to have a date selection model, as you show in your example. I bought your book, but it has the same info as this web page. I’m assuming this is more complicated, since it it has several components (text field, spinner, and DateChooserPanel). Do I have to create some kind of listener for each component?
Thanks,
Amy
Yair,
You are amazing! Thank you! This worked like a champ! Maybe this will go into the next edition of the book?
Amy
@Amy – maybe
I’m not sure at the moment if and when a second edition of my book will be published. A lot will depend on the question of how much will the upcoming Matlab 8 change the Java internals and interfaces.
In the meantime I’ve already started working on an altogether different Matlab programming book, and this is taking up much of my time…
Hi,
First of all thanks for posting this. I’m trying to add a DateComboBox in my GUI, but I need to be able to make some custom dates non selectable (and if possible also display them in another color).
I think I need to override some of the java code (I found an example in jide forum), but I don’t know if it’s possible in Matlab. If it’s possible, how do I do it?
This is the code I found:
@Carlos – I’m not sure you can customize the controls this way. However, I think you can use something similar to this (untested):
Alternately, you can create a Java class that implements
com.jidesoft.combobox.DateFilter. This is a simple interface that just defines 3 methods:Once you compile this class, you can add an object of it to the jDateChooser via
Thanks a lot! It worked and it also changes de color of the non selectable dates to gray.
But the correct method was addInvalidDate instead of removeInvalidDate. removeInvalidDate is for making the invalid date selectable again.
I found out that you can also set a min date and a max selectable date easily with setMaxDate or setMinDate (calling them in the same way that addInvalidDate).
I’ve just seen your edited comment. I want to change the selectable dates during the execution of my program depending on several things so I guess it’d be more simple to just use addInvalidDate, setMinDate and setMaxDate.
Thanks a lot!
It would be possible to show an example in java which display multiple dates selected, for example if the dates are taken from the db and need to be shown on the calendar
Hey Yair, I am following your guide and I ran into a problem.
I did exactly the same as you wrote, and everything was good until the part where i wanted to select
multiple dates on a calendar. But when I type in this line:
jModel = hPanel.getSelectionModel;It says: “No appropriate method, property, or field getSelectionModel for class handle.handle.”
What am I doing wrong? I’ve tried everything.
Thanks in advance!
@Ivan – this means that the
hPanelwas not created properly, or perhaps was deleted by the time your program got to thejModelassignment line.So how do I fix that? I followed your guide and used exactly the same commands up to that point.
This would really help me a lot.
When I leave the figure window open and type the lines:
then it doesn’t report the error I mentioned above. But it still doesn’t change the ability to select more dates on a calendar.
I’m confused.
@Ivan – it is impossible to know exactly why this happens without debugging your code and knowing more details about your environment (platform, Matlab release, java version – basically what ver reports). Contact me via email (altmany at gmail) with all the details and I’ll send you a proposed quote for solving this.