title: De-Highlight the dates in JCalendar using JComboBox
body: <p>I am making a Java GUI with MySQL database for attendance recording. I have created a <code>JFrame</code> for adding new attendance. When user selects a subject name from the <code>JComboBox</code> the code verifies the subject name then gets the dates from the database and highlight the dates in <code>JCalendar</code> showing the user about that particular subject's lecture days.</p>
  <p>The issue I am facing is that when the user selects another item/subject from the combo box, the frame should de-highlight the previous dates and only highlight the new ones. I am struggling with it. Haven't found anything on it.</p>
  <p><em><strong>My Code:</strong></em></p>
  <p><strong>(<code>HighlightTest</code> Class)</strong></p>
  <pre><code>import com.toedter.calendar.IDateEvaluator;
  import java.awt.Color;
  import java.util.ArrayList;
  import java.util.Date;
  import java.util.List;
  
  
  public class HighlightTest {
  
      public static class HighlightEvaluator implements IDateEvaluator {
  
          private final List&lt;Date&gt; list = new ArrayList&lt;&gt;();
  
          public void add(Date date) {
              list.add(date);
          }
  
          @Override
          public boolean isSpecial(Date date) {
              return list.contains(date);
          }
  
          @Override
          public Color getSpecialForegroundColor() {
              return Color.WHITE;
          }
  
          @Override
          public Color getSpecialBackroundColor() {
              return Color.GREEN;
          }
  
          @Override
          public String getSpecialTooltip() {
              return &quot;Highlighted event.&quot;;
          }
  
          @Override
          public boolean isInvalid(Date date) {
              return false;
          }
  
          @Override
          public Color getInvalidForegroundColor() {
              return null;
          }
  
          @Override
          public Color getInvalidBackroundColor() {
              return null;
          }
  
          @Override
          public String getInvalidTooltip() {
              return null;
          }
      }
      
  }
  </code></pre>
  <p><strong>(Main <code>JFrame</code> Class)</strong></p>
  <pre><code>import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.ResultSet;
  import java.sql.Statement;
  import java.util.Calendar;
  import java.util.Date;
  
  
  public final class framer extends javax.swing.JFrame {
  
      /**
       * Creates new form framer
       */
      public void subjectsFillData(){
          try{
              Class.forName(&quot;com.mysql.cj.jdbc.Driver&quot;);
              Connection con = DriverManager.getConnection(&quot;jdbc:mysql://localhost:3306/dbdb?useSSL=false&quot;,&quot;root&quot;,&quot;&quot;);            
              Statement stat = con.createStatement();    
              String selectQuery=&quot;select SubjectName from subjects&quot;;
              ResultSet rs=stat.executeQuery(selectQuery);
              while(rs.next()){
                  subjects.addItem(rs.getString(&quot;SubjectName&quot;));
                  
              }
          }
          catch(Exception e){
              System.out.println(e);
          }
      }
      
      private Date createDate(int d) {
          Calendar c = Calendar.getInstance();
          c.set(Calendar.DAY_OF_MONTH, d);
          c.set(Calendar.HOUR_OF_DAY, 0);
          c.set(Calendar.MINUTE, 0);
          c.set(Calendar.SECOND, 0);
          c.set(Calendar.MILLISECOND, 0);
          return (c.getTime());
      }
      
      public framer() {
          initComponents();
          subjectsFillData();
      }
  
      /**
       * This method is called from within the constructor to initialize the form.
       * WARNING: Do NOT modify this code. The content of this method is always
       * regenerated by the Form Editor.
       */
      @SuppressWarnings(&quot;unchecked&quot;)
      // &lt;editor-fold defaultstate=&quot;collapsed&quot; desc=&quot;Generated Code&quot;&gt;                          
      private void initComponents() {
  
          calEn = new com.toedter.calendar.JCalendar();
          sButton = new javax.swing.JButton();
          subjects = new javax.swing.JComboBox&lt;&gt;();
  
          setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  
          sButton.setText(&quot;Show&quot;);
          sButton.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  sButtonActionPerformed(evt);
              }
          });
  
          subjects.addItemListener(new java.awt.event.ItemListener() {
              public void itemStateChanged(java.awt.event.ItemEvent evt) {
                  subjectsItemStateChanged(evt);
              }
          });
  
          //java-swing-code-here      
  
      private void sButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
          String subject = (String) subjects.getSelectedItem();
          try{
              Class.forName(&quot;com.mysql.cj.jdbc.Driver&quot;);
              Connection con = DriverManager.getConnection(&quot;jdbc:mysql://localhost:3306/dbdb?useSSL=false&quot;,&quot;root&quot;,&quot;&quot;);            
              Statement stat = con.createStatement(); 
              
              String searchQuery=&quot;select * from &quot;+subject+&quot; &quot;;
              ResultSet rs=stat.executeQuery(searchQuery);
              while (rs.next()){
                 String ds = rs.getString(2);
                 String s = ds.split(&quot;-&quot;)[0];
                 int d = Integer.parseInt(s);
                 
                 HighlightTest.HighlightEvaluator evaluator = new HighlightTest.HighlightEvaluator();
                 evaluator.add(createDate(d));
                 evaluator.add(createDate(d));
                
                 calEn.getDayChooser().addDateEvaluator(evaluator);
                 calEn.setCalendar(calEn.getCalendar());
                  
              }
          }
          catch(Exception e){
              System.out.println(e);
          }
      }                                       
  
      private void subjectsItemStateChanged(java.awt.event.ItemEvent evt) {                                          
          **// Check if the item is changed and de-highlight previous dates and highlight new ones.**
          
      }                                         
  
      /**
       * @param args the command line arguments
       */
      public static void main(String args[]) {
          
           
          //GUIeditorfold-code-here
  
          /* Create and display the form */
          java.awt.EventQueue.invokeLater(new Runnable() {
              public void run() {
                  new framer().setVisible(true);
              }
          });
      }
  
      // Variables declaration - do not modify                     
      private com.toedter.calendar.JCalendar calEn;
      private javax.swing.JButton sButton;
      private javax.swing.JComboBox&lt;String&gt; subjects;
      // End of variables declaration                   
  }
  </code></pre>
  <p><strong>GUI:</strong></p>
  <p><a href="https://i.stack.imgur.com/R2b2R.jpg" rel="nofollow noreferrer">JFrame</a></p>