0

I have two JTables tblOrderInfo and tblDetailInfo i want to color the tblOrderInfo rows on the basis of tblDetailInfo rows, i have done lots of research and found Rob Camick's article and many other articles but it works for the Coloring based on static values checking and does not work for my case for dynamic filtering of JTable.

I tried a function using Rob's approach but it not works.

    private Component createColoring(DefaultTableModel model)
    {
    tblOrderInfo = new JTable( model )
    {
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
    {
    Component c = super.prepareRenderer(renderer, row, column);

    //  Color row based on a cell value

    if (!isRowSelected(row))
    {
        c.setBackground(getBackground());
        int modelRow = convertRowIndexToModel(row);
        String type = (String)getModel().getValueAt(modelRow, 0);


    Object orderId="";
    Object design="";
    Object sno="";                     

    for(int r=0;r< tblDetailInfo.getRowCount();r++){
    orderId= tblDetailInfo.getValueAt(r,  util.getColumnIndex( tblDetailInfo, "orderId"));
    design= tblDetailInfo.getValueAt(r,  util.getColumnIndex( tblDetailInfo, "design"));
    sno= tblDetailInfo.getValueAt(r,  util.getColumnIndex( tblDetailInfo, "sno"));

    for(int o=0;o< tblOrderInfo.getRowCount();o++){
    if(( tblOrderInfo.getValueAt(o,  util.getColumnIndex( tblOrderInfo, "orderId")).equals(orderId))
    && ( tblOrderInfo.getValueAt(o,  util.getColumnIndex( tblOrderInfo, "design")).equals(design))
    && ( tblOrderInfo.getValueAt(o,  util.getColumnIndex(tblOrderInfo, "sno")).equals(sno))

    ){ 
    c.setForeground(Color.RED);




    }
    }


    }
    }

    return c;
    }
    };

    tblOrderInfo.setPreferredScrollableViewportSize(tblOrderInfo.getPreferredSize());
    tblOrderInfo.changeSelection(0, 0, false, false);
    tblOrderInfo.setAutoCreateRowSorter(true);
    return tblOrderInfo;
    }

The calling code is below

 DefaultTableModel dtm =(DefaultTableModel)    tblOrderInfo.getModel();
  jScrollPane2.remove(tblOrderInfo);
   jScrollPane2.add(createColoring(dtm)); 

2 Answers2

1

The prepareRenderer() method is called for every cell so your basic code structure is wrong. You loop throw all the row in one table and then all the rows in the second table. So the row will colored if a match is found anywhere and it will be the same for every row in the table.

i want to color the tblOrderInfo rows on the basis of tblDetailInfo rows,

I think your basic pseudo code should be:

foreach row in the detail table

    if ((detail.orderId.equals(order.orderid)
    and (detail.design.equals(order.design)
    and (detail.sno.equals(order.sno))
    {
        c.setForeground( Color.RED);
        break;    
    }
camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1
    Thanks @Camickr i admit my mistake. My algo was wrong break was missing. Thanks for pointing my mistake. – Syed Muhammad Mubashir Oct 07 '13 at 17:26
  • Dear @Camickr tested with break logic again but it does not work and all the rows of order table are colored. – Syed Muhammad Mubashir Oct 08 '13 at 03:27
  • I suggested you need to make more changes than just add the break. I suggested you don't need 2 for loops. I can't write the code for you. – camickr Oct 08 '13 at 03:46
  • Your logic is wrong somewhere. Unless you can post a proper `SSCCE`, I can't make anymore suggestions because I don't know if you have implemented my suggestions properly, or if I understand your exact requirement. – camickr Oct 08 '13 at 05:05
0

The following class works for me

  class MyTableCellRenderer extends DefaultTableCellRenderer implements TableCellRenderer {
 AssignOrderToWax ass =null;

 public MyTableCellRenderer( AssignOrderToWax ass){
     this.ass=ass;
 }
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        setForeground(Color.BLACK);
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);


        Object orderId="";
        Object design="";
         Object sno="";
        for(int r=0;r<ass.tblDetailInfo.getRowCount();r++){
         orderId=ass.tblDetailInfo.getValueAt(r, ass.util.getColumnIndex(ass.tblDetailInfo, "orderId"));
         design=ass.tblDetailInfo.getValueAt(r, ass.util.getColumnIndex(ass.tblDetailInfo, "design"));
         sno=ass.tblDetailInfo.getValueAt(r, ass.util.getColumnIndex(ass.tblDetailInfo, "sno"));

if((ass.tblOrderInfo.getValueAt(row, ass.util.getColumnIndex(ass.tblOrderInfo, "orderId")).equals(orderId))
     && (ass.tblOrderInfo.getValueAt(row, ass.util.getColumnIndex(ass.tblOrderInfo, "design")).equals(design))
     && (ass.tblOrderInfo.getValueAt(row, ass.util.getColumnIndex(ass.tblOrderInfo, "sno")).equals(sno))

        ){ 
            setForeground(Color.RED);
            break ;

            }

    }return this;
    }

}

Below is the calling code.

      Enumeration<TableColumn> en = tblOrderInfo.getColumnModel().getColumns();
    while (en.hasMoreElements()) {
       tc = en.nextElement();
        tc.setCellRenderer(new MyTableCellRenderer(this));
    }