JTable设置TableCellRenderer后默认显示样式全成了JLabel,本来里面有boolean类型的显示的是JCheckBox的~有时间显示的是时间的样式的,我用TableCellRenderer设置了背景颜色~~~这个是设置后效果
这个是设置前的效果
现在想让它既能显示背景颜色 又能正确显示boolean的单选框怎么办啊?
//这是设置renderer的代码!
public void setTableRenderer(JTable table) {
TableColumnModel tcm = table.getColumnModel();
for (int i = 0, n = tcm.getColumnCount(); i < n; i++) {
TableColumn tc = tcm.getColumn(i);
tc.setCellRenderer(new RowRenderer());
}
}
public class RowRenderer extends DefaultTableCellRenderer {
public RowRenderer() {
super();
}
public Component getTableCellRendererComponent(JTable t, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
// 设置奇偶行的背景色,可在此根据需要进行修改
if (row % 2 == 0)
setBackground(Color.WHITE);
else
setBackground(new Color(238, 238, 238));
return super.getTableCellRendererComponent(t, value, isSelected,
hasFocus, row, column);
}
}下面是AbstractTableModel中getColumnClass方法
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}求大神帮帮忙~~~~~~~~jtablebooleancolor
这个是设置前的效果
现在想让它既能显示背景颜色 又能正确显示boolean的单选框怎么办啊?
//这是设置renderer的代码!
public void setTableRenderer(JTable table) {
TableColumnModel tcm = table.getColumnModel();
for (int i = 0, n = tcm.getColumnCount(); i < n; i++) {
TableColumn tc = tcm.getColumn(i);
tc.setCellRenderer(new RowRenderer());
}
}
public class RowRenderer extends DefaultTableCellRenderer {
public RowRenderer() {
super();
}
public Component getTableCellRendererComponent(JTable t, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
// 设置奇偶行的背景色,可在此根据需要进行修改
if (row % 2 == 0)
setBackground(Color.WHITE);
else
setBackground(new Color(238, 238, 238));
return super.getTableCellRendererComponent(t, value, isSelected,
hasFocus, row, column);
}
}下面是AbstractTableModel中getColumnClass方法
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}求大神帮帮忙~~~~~~~~jtablebooleancolor
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货