改变JTable行的颜色

时间:2013-10-07 12:19:10

标签: java swing jtable tablecellrenderer

我有两个JTables tblOrderInfo和tblDetailInfo我想在tblDetailInfo行的基础上为tblOrderInfo行着色,我做了很多研究并找到了Rob Camick's文章和许多其他文章,但它适用于基于着色的着色静态值检查,对于我的JTable动态过滤的情况不起作用。

我使用Rob的方法尝试了一个函数,但它不起作用。

    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;
    }

调用代码位于

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

2 个答案:

答案 0 :(得分:1)

为每个单元格调用prepareRenderer()方法,因此您的基本代码结构是错误的。你循环抛出一个表中的所有行,然后抛出第二个表中的所有行。因此,如果在任何地方找到匹配,则该行将着色,并且对于表中的每一行它将是相同的。

  

我想根据tblDetailInfo行为tblOrderInfo行着色,

我认为你的基本伪代码应该是:

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;    
    }

答案 1 :(得分:0)

以下课程适合我

  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;
    }

}

以下是主叫代码。

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