0

In the answer to my previous question, Coloring jTable row, but now I'm unsure as to new question goes here, setting color to row is working. But I want to give it from a for loop, means I want to set color for i'th row. I'm giving the which I have used,

for(int i=0;i<serialNumber;i++){
  if((jTable1.getValueAt(i,1).toString()).equals(BidNumber)){
   Enumeration<TableColumn> en = jTable1.getColumnModel().getColumns();
    while (en.hasMoreElements()) {
        TableColumn tc = en.nextElement();

        tc.setCellRenderer(new MyTableCellRenderer());
    }
}

It will invoke the method cellrenderer,

public class MyTableCellRenderer extends DefaultTableCellRenderer implements TableCellRenderer {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            setBackground(null);
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

              boolean interestingRow = row ==1;



            if (interestingRow) {
                setBackground(Color.YELLOW);
            }

            return this;
        }

    }

here I am trying to put color for the row when first column value is equal to the BidNumber, but in cellrenderer(here),it's set as row==1,then every time when the condition is true the 1 st row will be colored..How can I set it for i'th row?

Community
  • 1
  • 1
Luna
  • 956
  • 4
  • 15
  • 28

1 Answers1

2

Change boolean interestingRow = row ==1 to meet your requirements, where row == n would produce the desired result. You must define n in some meaningful way

This could come from the table data model or be supplied by other means

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366