java JTable设置某一行的颜色

来源:互联网 发布:淘宝如何申请 编辑:程序博客网 时间:2024/05/03 20:04

java JTable设置某一行的颜色代码如下:



/** * 设置表格的某一行的背景色 * @param table */public static void setOneRowBackgroundColor(JTable table, int rowIndex,Color color) {try {DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {public Component getTableCellRendererComponent(JTable table,Object value, boolean isSelected, boolean hasFocus,int row, int column) {if (row == rowIndex) {setBackground(color);setForeground(Color.WHITE);}else if(row > rowIndex){setBackground(Color.BLACK);setForeground(Color.WHITE);}else{setBackground(Color.BLACK);setForeground(Color.WHITE);}return super.getTableCellRendererComponent(table, value,isSelected, hasFocus, row, column);}};int columnCount = table.getColumnCount();for (int i = 0; i < columnCount; i++) {table.getColumn(table.getColumnName(i)).setCellRenderer(tcr);}} catch (Exception ex) {ex.printStackTrace();}}




1 0
原创粉丝点击