import java.awt.Color; import javax.swing.JTable; import javax.swing.table.*; public class ColorTable extends JTable { /** Creates a new instance of ColorTable */ private DefaultTableCellRenderer oddRenderer; private DefaultTableCellRenderer evenRenderer; public ColorTable() { super(); } public ColorTable(TableModel tm) { super(tm); } public ColorTable(Object[][] data, Object[] columns) { super(data, columns); } public ColorTable(int rows, int columns) { super(rows, columns); } /** * If row is an even number, getCellRenderer() returns a DefaultTableCellRenderer * with white background. For odd rows, this method returns a DefaultTableCellRenderer * with a light gray background. */ @Override public TableCellRenderer getCellRenderer(int row, int column) { TableCellRenderer cellRenderer; if (oddRenderer == null) { oddRenderer = new DefaultTableCellRenderer(); } if (evenRenderer == null) { evenRenderer = new DefaultTableCellRenderer(); evenRenderer.setBackground(new Color(181, 230, 29)); } cellRenderer = ((row%2)==0)? oddRenderer : evenRenderer; return cellRenderer; } }
Kéo một JTable vào trong form (NetBean), sau đó trong phần Custom Creation Code thêm dòng
new ColorTable()Thế là xong :D
tuyệt quá, đúng thứ mình cần, cảm ơn cậu rất nhiềuuuuuuuuuuuuuuuu
ReplyDelete