JTable: How to get the cell as a component for a given position

Viewed 3858

I made an interactive Calendar as JTable, however, I would like to change the background colour for some of the cells based on data I have. I figured out how I can get the position of the cell I need to edit but I have no idea how to get the cell at that position as a component so I can edit the cell background and foreground.

So basically I have the (x,y) position of the cell. I want to use that and get the cell to change its background colour.

This is how I create my calendar:

public static JTable createInteractiveCalender(int month, int year) {       

    JTable calender =  new JTable(Calender.getMonthsCalender(month, year), new String[] {"Su","Mo","Tu","Wed","Th","Fri","Sat"}){
         public boolean isCellEditable(int row, int column) {                
                return false;               
        };
    };
    calender.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    calender.setCellSelectionEnabled(true);

    return calender;
}

This is how I get the dates I want to be marked:

public static ArrayList<Date> getDatesInSpecificMonth(ArrayList<Date> allDates, int month) {

DateFormat df = new SimpleDateFormat("MM");
ArrayList<Date> dates = allDates;
for (Date d: dates)
    if(Integer.parseInt(df.format(d)) != month)
        dates.remove(d);
    return dates;
}

This is where I got stuck trying to mark the dates on the calendar:

public static void markDatesOnCalender(DefaultTableModel model, Section sec, int datesToMark, int month, int year) {
        ArrayList<Date> dates = Calender.getDatesInSpecificMonth(sec.getSelectedDatesforSection(datesToMark),month);
        DateFormat df = new SimpleDateFormat("dd");

        for (Date d: dates) {
            model.getValueAt(getCellPosition(model,df.format(d))[0],getCellPosition(model, d

f.format(d))[1]);



/*
         I have method that gets the cell position of 'd', however I need to get the                    
         */
        }                       
    //TODO 1
}

Thank you

1 Answers
Related