JAVAJTable奇数行和偶数行设定显示不同颜色
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
JAVAJTable奇数行和偶数行设定显示不同颜色
publicclass JTableDemo extendsJTable {
private Color selectionColor=newColor(207,228,249);//行选择颜色private Color evenRowColor=newColor(233,242,241);//奇数行颜色private Color oddRowColor=newColor(255,255,255);//偶数行颜色private Color
gridColor=newColor(236,233,216);//网格颜色privateintrowHeight= 30;//行高度
publicJTableDemo(TableModel tableModel){
}super(tableModel);
this.setGridColor(gridColor);
this.setRowHeight(rowHeight);
publicTableCellRender getCellRender(introw,intcolumn)
{}returnnewMyCellRender();
class MyCellRender extendsDefaultTableCellRender{
public Component getTableCellRenderComponent(JTable table,Object
value,booleanisSelected,booleanhasFocus,introw,intcolumn){ Component cell =
super.getTableCellRenderComponent(table,value,isSelected, hasFocus,row,colu mn);
this.setColor(cell, table, isSelected, hasFocus, row, column);returncell;}/*
*设置颜色
*/
if(isSelected){
setBorder(null);//去掉边
}else{
}if(row%2 == 0){
}else{
publicstaticvoidmain(String[] args){
JFrame frame =newJFrame("java Swing Jtable Demo");
//初始化测试数据
String[] tableTitleArray = {"ID","Name","Sex"};
Object[][] body =newObject[6][tableTitleArray.length];for(inti = 0; i < 6;i++){ body[i][0] = i;
body[i][1] ="xx";
body[i][2] ="男";}JT ableDemo table =newJTableDemo(new DefaultTableModel(body,tableTitleArray));
frame.getContentPane().add(new
JScrollPane(table),BorderLayout.CENTER);
}//设置JFrame属性
}frame.setDefaultCloseOperation(WindowConstants.EXIT_O N_CLOSE);frame.set Size(640,480);
frame.setLocationRelativeTo(null);
frame.setVisible(true);。