1

I have highlighted required dates in JDateChooser(version: jcalendar 1.4) by adding IDateEvaluator. Whenever I add new entries, I execute this class to highlight the new set of dates. This is working fine. But when I delete an entry, its still showing the respective date highlighted. Is there a fix for this?

my main fuction:

    public void run() {
    HighlightEvaluator evaluator = new HighlightEvaluator();
     for(Date filled : getFilledDates())  {  
         evaluator.add(filled);
     }
     dateChooser.getJCalendar().getDayChooser().addDateEvaluator(evaluator);
}

my dateevaluator class:

    private static class HighlightEvaluator implements IDateEvaluator {
    private final List<Date> list = new ArrayList<>();
    public void add(Date date) {
        list.add(date);
    }

    @Override
    public Color getInvalidBackroundColor() {
        return Color.BLACK;
    }

    @Override
    public Color getInvalidForegroundColor() {
        return null;
    }

    @Override
    public String getInvalidTooltip() {
        return null;
    }

    @Override
    public Color getSpecialBackroundColor() {
        return Color.GREEN;
    }

    @Override
    public Color getSpecialForegroundColor() {
        return Color.RED;
    }

    @Override
    public String getSpecialTooltip() {
        return "filled";
    }

    @Override
    public boolean isInvalid(Date date) {
        return false;
    }

    @Override
    public boolean isSpecial(Date date) {
        return list.contains(date);
    }
}

the run() function runs everytime the panel is loaded and it checks for set of text files in various folders and highlights those dates

here is my getFilledDates() function:

    private List<Date> getFilledDates() {
    List<Date> filledDates = new ArrayList<Date>();
    String filename = StorageSpace.currentpath;
    File folder = new File(filename);
    String[] yearfolders = folder.list(new FilenameFilter() {                   
        @Override
        public boolean accept(File dir, String name) {
            return new File(dir, name).isDirectory();
        }
    });
    for(int i=0; i<yearfolders.length;i++) {
        folder = new File(filename + "\\" + yearfolders[i]);
        if(folder.exists()) {
            String[] monthfolders = folder.list(new FilenameFilter() {                  
                @Override
                public boolean accept(File dir, String name) {
                    return new File(dir, name).isDirectory();
                }
            });
            for(int j=0;j<monthfolders.length;j++) {
                folder = new File(filename + "\\" + yearfolders[i] + "\\" + monthfolders[j]);
                if(folder.exists()) {
                    File[] listOfFiles = folder.listFiles();
                    for (int n = 0; n < listOfFiles.length; n++) {
                         Calendar c = Calendar.getInstance();
                         c.set(Calendar.YEAR, Integer.parseInt(yearfolders[i]));
                         c.set(Calendar.MONTH, Integer.parseInt(monthfolders[j])-1);
                         c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(listOfFiles[n].getName().replaceAll(".txt", "")));
                         c.set(Calendar.HOUR_OF_DAY, 0);
                         c.set(Calendar.MINUTE, 0);
                         c.set(Calendar.SECOND, 0);
                         c.set(Calendar.MILLISECOND, 0);
                         filledDates.add(c.getTime());
                    }
                }
            }
        }
    }
    return filledDates;
}

Now everytime a file is added, the entire folders and files are scanned again and the latest set of dates are highlighted. But when a file is deleted, the date is still in green colour although that particular date is not in the list returned by getFilledDates() and its definitely not added to the evaluator.

So this is my issue

Hari Kiran
  • 188
  • 13
  • Not sure wiothout seeing your code but perhaps is the buggy behavior described in [this answer](https://stackoverflow.com/a/25695372/1795530)? – dic19 Sep 27 '18 at 13:10
  • @dic19 I updated the code. Do check and see if you understand the problem – Hari Kiran Sep 27 '18 at 15:27
  • 1
    1. In your `run()` method is clear that you add the dates to the evaluator's list but you don't remove any previous date added, so it makes sense that the new ones and the old ones are highlighted. 2. IMO you shouldn't add the evaluator every time `run()` method is called but work with the same instance, i.e. by holding a reference to the evaluator. 3. If all dates can be retrieved in a single list then it'd make sense to provide a `setDates(listOfDates)` method in your custom evaluator instead of adding one date at the time. – dic19 Sep 27 '18 at 16:44
  • @dic19 I tried your solution by using the same instance of evaluator. run() method is run only once and I am adding dates and removing by using a reference to the evaluator. But its not reflecting any changes. Do I have to update the UI of JDateChooser component? – Hari Kiran Sep 28 '18 at 12:11
  • the respective dates added to evaluator istance are being highlighted but the ones removed are not altered and still highlighted. please give any update @dic19 – Hari Kiran Oct 12 '18 at 10:50

0 Answers0