我用的是jb,请问如何根据JdtTable的行内容来改变该行颜色。比如该行中某个字段为空,该行显示为红色

解决方案 »

  1.   

    不知道楼主用的那个控件是什么
    我用的JTable 根据条件设置行的颜色 
     
    其中第三列是Y显示一种颜色
    第四列是Y的显示另一种颜色
     
     
    public static boolean compare(int a, String b) {       
        int i;
        String d = "";
        int[] c;
        for (i = 0; i < b.length()-1; i++) {
          d = b.substring(i, i + 2);
          i++;
          if (a == Integer.parseInt(d))
            return true;
        }
        return false;
      }
      public static void makeFace(JTable table) {
        try {
          DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
            public Component getTableCellRendererComponent(JTable table,
                Object value, boolean isSelected, boolean hasFocus,
                int row, int column) {
              String red = "";
              String yellow = "";
              int i;
              for (i = 0; i < table.getRowCount(); i++) {
                if (table.getValueAt(i, 2).equals("Y")) {
                  if (i > 9) {
                    red = red + String.valueOf(i);
                  }
                  else {
                    red = red + String.valueOf(0) + String.valueOf(i);
                  }
                }
              }
              for (i = 0; i < table.getRowCount(); i++) {
                if (table.getValueAt(i, 3).equals("Y")) {
                  if (i > 9) {
                    yellow = yellow + String.valueOf(i);
                  }
                  else {
                    yellow = yellow + String.valueOf(0) + String.valueOf(i);
                  }
                }
              }
              if (compare(row, red))
                setBackground(Color.red);
              else if (compare(row, yellow))
                setBackground(Color.yellow);
              else
                setBackground(Color.white);
              return super.getTableCellRendererComponent(table, value,
                  isSelected, hasFocus, row, column);
            }
          };
          for (int i = 0; i < table.getColumnCount(); i++) {
            table.getColumn(table.getColumnName(i)).setCellRenderer(tcr);
          }
        }
        catch (Exception ex) {
          ex.printStackTrace();
        }
      }