JDateChooser setDate call ends execution unexpectedly
I am using Toeder JDateChooser
in my stand-alone Java application as seen in the code snippet b开发者_StackOverflow中文版elow. The purpose here is:
- To shift the the calendar instance to a desired week of the year and then
- To set the start-date and the end-date of the desired week accordingly. These dates are kept in two JDateChooser instances, namely
jDateChooserBookBegin
andjDateChooserBookEnd
.
Calendar c = Calendar.getInstance();
int currentWeekOfYear = c.get(Calendar.WEEK_OF_YEAR);
int desiredWeekOfYear = jComboBookWeekMainPanel.getSelectedIndex() + 1;
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
for (int i = currentWeekOfYear; i < desiredWeekOfYear; i++) {
c.add(Calendar.DAY_OF_WEEK, 7); //e.g. set to next Monday
}
jDateChooserBookBegin.setDate(c.getTime()); // ***** HERE *****
c.add(Calendar.DAY_OF_WEEK, 6); //set to the end of week e.g. Sunday
jDateChooserBookEnd.setDate(c.getTime());
Problem: Let's say the above snippet is executed within a method. I checked the code in debug mode in neatbeans and I swear when the execution reaches the line marked with a dashed arrow, the rest of the lines are not executed at all and the encapsulating method returns immediately to the caller. This causes jDateChooserBookEnd
not to be set to the proper date and my program behaves in an undesired fashion as a result.
Question: Why does the setDate
method of the JDateChooser
class cause the rest of the lines not to be executed by returning to the caller? Is there a known bug on this? Do you have any clue? This looks rather impossible but it's happening. I am using Java 6.
The full method content is available below. The above snippet is a simplified version of what you see below. In the program, I have two jButtons named next week and previous week. Whenever one of these buttons is clicked the relevant actionPerformed method makes a call to the below method.
private void update_DateFieldsInMainPanel() {
Calendar c = Calendar.getInstance();
int currentWeekOfYear = c.get(Calendar.WEEK_OF_YEAR);
int desiredWeekOfYear = jComboBookWeekMainPanel.getSelectedIndex() + 1; c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
if (desiredWeekOfYear ==currentWeekOfYear) {
jDateChooserBokMainFrom.setDate(c.getTime());
c.add(Calendar.DAY_OF_WEEK, 6);
jDateChooserBokMainTill.setDate(c.getTime());
}
else if (desiredWeekOfYear > currentWeekOfYear) {
for (int i = currentWeekOfYear; i < desiredWeekOfYear; i++) {
c.add(Calendar.DAY_OF_WEEK, 7);
}
jDateChooserBokMainFrom.setDate(c.getTime());
c.add(Calendar.DAY_OF_WEEK, 6);
jDateChooserBokMainTill.setDate(c.getTime());
}
else {
for (int i = currentWeekOfYear; i > desiredWeekOfYear; i--) {
c.add(Calendar.DAY_OF_WEEK, -7);
}
jDateChooserBokMainFrom.setDate(c.getTime());
c.add(Calendar.DAY_OF_WEEK, 6);
jDateChooserBokMainTill.setDate(c.getTime());
}
}
Try to catch Throwable instead of Exception.
精彩评论