改变JTable一行的颜色

改变JTable一行的颜色
这个可以参考一下
DefaultTableCellRenderer dtc = new DefaultTableCellRenderer(){ public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if(row%2 == 0) setForeground(Color.red); else setForeground(Color.green); return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); } }; table.getColumnModel().getColumn(1).setCellRenderer(dtc); table.getColumnModel().getColumn(2).setCellRenderer(dtc);
下面的代码是使文本框左对齐
import java.awt.*; import javax.swing.*; import javax.swing.table.*; import javax.swing.table.DefaultTableCellRenderer; public class Renderer extends DefaultTableCellRenderer { public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { this.setHorizontalAlignment(SwingConstants.RIGHT); return super.getTableCellRendererComponent(table, value,isSelected, hasFocus, row, column) ; } }

你可能感兴趣的:(改变JTable一行的颜色)