1

I am using Toeder JDateChooser in my stand-alone Java application as seen in the code snippet below. The purpose here is:

  1. To shift the the calendar instance to a desired week of the year and then
  2. To set the start-date and the end-date of the desired week accordingly. These dates are kept in two JDateChooser instances, namely jDateChooserBookBegin and jDateChooserBookEnd.
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());  
    }
}
Kai
  • 38,985
  • 14
  • 88
  • 103
farda
  • 11
  • 2
  • Is this done in a try/catch block? If so, are you catching exceptions? Do any exceptions get thrown? – Hovercraft Full Of Eels Sep 20 '11 at 22:09
  • No try/catch is present and absolutely no exception is thrown. – farda Sep 20 '11 at 22:12
  • This is smelling like an exception is occurring somewhere somehow. But with just the information presented, it's hard to say. – Hovercraft Full Of Eels Sep 20 '11 at 22:16
  • Hard to say without your [sscce](http://sscce.org/), but you might try the fixes mentioned [here](http://stackoverflow.com/tags/jcalendar/info). – trashgod Sep 20 '11 at 22:46
  • I have two jButtons like next week and previous week. Whenever one of these buttons is clicked the relevant actionPerformed method makes a call to the below method. The code snippet I had written previously is a simplified version of this method. The setDate method normally does not throw an exception, I did not check anything on that within a try/catch. Perhaps you can think of a runtime exception which interrupts the entire program, which is not the case in the program I wrote. – farda Sep 20 '11 at 23:23
  • 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()); – farda Sep 20 '11 at 23:24
  • } 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()); – farda Sep 20 '11 at 23:24
  • c.add(Calendar.DAY_OF_WEEK, 6); jDateChooserBokMainTill.setDate(c.getTime()); } } – farda Sep 20 '11 at 23:25
  • I am sorry the page did not let me put the whole method code in one comment. – farda Sep 20 '11 at 23:26
  • You can edit your question to include revised code; this will make it easier to read, too. – trashgod Sep 21 '11 at 01:43

1 Answers1

0

Try to catch Throwable instead of Exception.

Gergely Bacso
  • 14,243
  • 2
  • 44
  • 64