问题描述
我正在编写的程序允许用户单击JXDatePicker上的日期来设置任务完成的日期.我想在JXDatePicker中禁用对未来日期的选择,因为在我的程序中选择未来日期是无效的.
A program I'm writing allows users to click a date on a JXDatePicker to set the date a task has been completed. I would like to disable the selection of future dates in the JXDatePicker, since in my program selecting future dates is invalid.
我发现JXDatePickers包含JXMonthView,似乎日期选择器或月份视图似乎不允许您禁用单个日期范围.我可以更改单个日期和日期范围的背景色,这应该可以使将来的日期成为单独的颜色.然后,每当用户单击日历以禁止将来的日期时,我都可以添加一些验证.但是,如果我只说诸如calendar.setMaxDate(today);
I have found that JXDatePickers contain a JXMonthView, and it does not seem that date pickers or month views allow you to disable individual/ranges of dates. I can change the background coloring of individual dates and ranges of dates, which should allow me to make future dates a separate color. Then I could add in some validation whenever the user clicks the calendar to disallow future dates. However, it would be so much cleaner if I could just say something like calendar.setMaxDate(today);
有没有人比手工编码功能更容易做到这一点?也许另一个日期选择器组件可以解决此问题?
Does anyone know if there is an easier method of doing this than hand-coding the functionality? Perhaps another date picker component fixes this problem?
推荐答案
如果要将日期选择限制在一个范围内,则可以指定上下边界
If you want to restrict date selection to only a range, you can specify upper and lower boundries
JXDatePicker picker = new JXDatePicker();
Calendar calendar = picker.getMonthView().getCalendar();
// starting today if we are in a hurry
calendar.setTime(new Date());
picker.getMonthView().setLowerBound(calendar.getTime());
// end of next week
CalendarUtils.endOfWeek(calendar);
calendar.add(Calendar.WEEK_OF_YEAR);
picker.getMonthView().setUpperBound(calendar.getTime());
(摘自JavaDocs)
(Taken from the JavaDocs)
这基本上将限制从今天到星期几的有效的可选日期.
This will basically restrict the valid, selectable dates from today to the end of the week.
这篇关于在JXDatePicker/JXMonthView中禁用日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!