javaswingjtable设置单元格背景色问题

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

packageaaa;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Component;importjava.util.ArrayList;importjava.util.List;importjavax.swing.JFrame;importjavax.swing.JScrollPane;importjavax.swing.JTable;importjavax.swing.table.DefaultTableCellRenderer;classEvenOddRendererextendsDefaultTableCellRenderer{privatestaticfinallongserialVersionUID=1L;privateListString[]positioins;publicEvenOddRenderer(ListString[]_positioins){this.positioins=_positioins;}publicComponentgetTableCellRendererComponent(JTabletable,Objectvalue,booleanisSelected,booleanhasFocus,introw,intcol){//table-JTable//value-分配给[row,column]处单元格的值//isSelected-如果已选定单元格,则为true//hasFocus-如果单元格具有焦点,则为true//row-要呈现的单元格所在行//column-要呈现的单元格所在列for(inti=0;itable.getRowCount();i++){if(row==i){this.setBackground(Color.white);}}for(String[]rowAndCol:this.positioins){int_row=Integer.valueOf(rowAndCol[0]);int_col=Integer.valueOf(rowAndCol[1]);if(_row==row&&_col==col){this.setBackground(Color.yellow);}}this.setText(value.toString());returnthis;}}publicclassResizeTable{publicstaticvoidmain(Stringargs[]){finalStringcolumnNames[]={#,English,Roman};finalObjectrowData[][]={{1,one,I},{2,two,II},{4,tahree,III},{3,three,III},{5,thasdfree,III}};finalJTabletable=newJTable(rowData,columnNames);JScrollPanescrollPane=newJScrollPane(table);//坐标值String[]arr0={1,1};String[]arr1={1,0};String[]arr2={0,2};String[]arr3={2,2};String[]arr4={3,2};ListString[]_list=newArrayListString[]();_list.add(arr0);_list.add(arr1);_list.add(arr2);_list.add(arr3);_list.add(arr4);EvenOddRenderertablecellRender=newEvenOddRenderer(_list);table.setDefaultRenderer(Object.class,tablecellRender);JFrameframe=newJFrame(ResizingTable);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.add(scrollPane,BorderLayout.CENTER);frame.setBounds(400,200,600,200);frame.setVisible(true);}}

1 / 2
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功