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) { if (row % 2 == 0) { setBackground(Color.white); //设置奇数行底色 } else if (row % 2 == 1) { setBackground(new Color(206, 231, 255)); //设置偶数行底色 } if (Double.parseDouble(table.getValueAt(row, 11).toString()) > 0) { setBackground(Color.red); }
//如果需要设置某一个Cell颜色,需要加上column过滤条件即可 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(); } }
JTable 单独设置某一行颜色
最新推荐文章于 2021-02-24 03:56:15 发布