Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

setting diferent colors in different rows for JTable

843804Mar 25 2005 — edited Jan 20 2008
dear all,
in the below codes, i do not know how to set different colors for rows with the different months and setting a same color for rows with the same month.

A simple illustration here:
-----Row-------Month--------Color of Row
-------0---------Jan-------------Green
-------1---------Jan-------------Green
-------2---------Feb-------------Blue
-------3---------Mar-------------Green
-------4---------Mar-------------Green
-------5---------Apr--------------Blue
-------6---------Apr--------------Blue

sorry for my weak programming knowledge, hope for forgiveness from all...
Thank you.

Codes:
public void rowColor() {
      int checkMonth, theMonth;
      Date checkDate, theDate;
     
      checkDate = new Date();  
      theDate = new Date();  
 
      //getting the date in first row
      checkDate = (Date)m_data.getValueAt(0,1);

      //getting month in the first row
      checkMonth = checkDate.getMonth();
                
      for(int i=1; i<m_data.getRowCount(); i++ ) {

        //getting the date and month in subsequent rows
        theDate = (Date)m_data.getValueAt(i,1);
        theMonth = theDate.getMonth();
        
        //Comparing the month information labelled in different rows
        if( theMonth == checkMonth ) {

            //if the rows are labelled with the same month
            //set the same row color
        }
        else {
            //set the row to another color if month information is found to be different
        }
        checkMonth = theMonth;
     }         
   }
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 17 2008
Added on Mar 25 2005
17 comments
356 views