1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
class LeTable extends JTable {
public LeTable() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private Color[][] m_cBgColor;
protected int m_iNbRow;
protected int m_iNbCol;
LeTable(Object[][] m_oDatas, String[] m_sTitres) {
super(new LeTableModel(m_oDatas, m_sTitres));
m_iNbRow = m_oDatas.length;
m_iNbCol = m_sTitres.length;
LeTableCellRenderer renderer = new LeTableCellRenderer();
this.setDefaultRenderer(Object.class, renderer);
this.setSelectionBackground(Color.YELLOW);
this.setSelectionForeground(Color.BLUE);
this.setGridColor(Color.black);
this.setRowHeight(20);
initColor();
}
protected void initColor() {
m_cBgColor = new Color[m_iNbRow][m_iNbCol];
for (int i = 0; i < m_iNbRow; i++) {
for (int j = 0; j < m_iNbCol; j++) {
m_cBgColor[i][j] = Color.WHITE;
}
}
}
/**
* setBackgroungColorAt
*
* @param c Color
* @param row int
* @param col int
*/
public void setBackgroungColorAt(Color c, int row, int col) {
m_cBgColor[row][col] = c;
}
private void jbInit() throws Exception {
}
class LeTableCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int col) {
Color BACKGROUND_SELECTED = Color.YELLOW;
Color FOREGROUND_SELECTED = Color.BLUE;
Color BACKGROUND = Color.WHITE;
Color FOREGROUND = Color.BLACK;
// on utilise le renderer super qui va tenir compte de
// tout ce qu'on a besoin (sélection, focus)
Component comp = super.
getTableCellRendererComponent(
table, value, isSelected, hasFocus, row,
col);
comp.setBackground(m_cBgColor[row][col]);
if (isSelected) {
this.setBackground(BACKGROUND_SELECTED);
this.setForeground(FOREGROUND_SELECTED);
this.setHorizontalAlignment(JLabel.CENTER);
} else {
//this.setBackground(BACKGROUND);
this.setForeground(FOREGROUND);
this.setHorizontalAlignment(JLabel.CENTER);
}
return this;
}
}
} |
Partager