4 个解决方案
#1
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)); //设置偶数行底色
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();
}
}
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)); //设置偶数行底色
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();
}
}
#2
ding
#3
自己订制一个表格绘制器
DefaultTableCellRenderer cellRender = new DefaultTableCellRenderer() {
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
//在这里根据行号,列号对单元格进行设置。
return super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);//这一句必须有
}
};
calendar.setDefaultRenderer(Object.class, cellRender);//calendar是个表格
DefaultTableCellRenderer cellRender = new DefaultTableCellRenderer() {
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
//在这里根据行号,列号对单元格进行设置。
return super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);//这一句必须有
}
};
calendar.setDefaultRenderer(Object.class, cellRender);//calendar是个表格
#4
可以按照某列的值来设置每一列的背景色么
比如type列的值为1时设置为红色,为2时设置为黄色
谢谢
比如type列的值为1时设置为红色,为2时设置为黄色
谢谢
#1
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)); //设置偶数行底色
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();
}
}
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)); //设置偶数行底色
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();
}
}
#2
ding
#3
自己订制一个表格绘制器
DefaultTableCellRenderer cellRender = new DefaultTableCellRenderer() {
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
//在这里根据行号,列号对单元格进行设置。
return super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);//这一句必须有
}
};
calendar.setDefaultRenderer(Object.class, cellRender);//calendar是个表格
DefaultTableCellRenderer cellRender = new DefaultTableCellRenderer() {
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
//在这里根据行号,列号对单元格进行设置。
return super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);//这一句必须有
}
};
calendar.setDefaultRenderer(Object.class, cellRender);//calendar是个表格
#4
可以按照某列的值来设置每一列的背景色么
比如type列的值为1时设置为红色,为2时设置为黄色
谢谢
比如type列的值为1时设置为红色,为2时设置为黄色
谢谢