当我向jtable中插入数据时,我测试cell的值是否为true,我如何改变这个cell的背景颜色
下面是我的代码:private void getEtudians(){
Etudiant e = new Etudiant();
DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
deleteTable(model);
ArrayList<Vector> ETUDIANTS = e.getAllEtudiansNom(2);
for (int i = 0; i <ETUDIANTS.size(); i++) {
System.out.print(i);
Vector v = new Vector();
v.add(i+1);
v.addAll(ETUDIANTS.get(i));
model.addRow(v);
}
jTable1.setModel(model);
}
尝试添加一个组件来聚焦它:
String[] header = { "ID", "Name", "Department" };
String[][] datas = { { "0001", "Onur", "Java Developer" }, { "0002", "Umut", "Musical Ens" },
{ "0003", "Eymen", "Just Reader" }, { "0004", "Mustafa", "Worker" } };
boolean isSelected = false;
boolean hasFocus = false;
JTable table = new JTable(datas, header);
int rows = table.getRowCount();
int cols = table.getColumnCount();
Object color = (Color) Color.CYAN;
table.setOpaque(false);
table.setFillsViewportHeight(true);
table.setCellSelectionEnabled(true);
table.setBounds(30, 40, 200, 300);
if (table.isCellSelected(rows, cols)) {
isSelected = true;
}
Component comp = getTableCellRendererComponent(table, color, isSelected, hasFocus, rows, cols);
if (rows == 1 && cols == 1)
comp.setBackground(new java.awt.Color(0, 0, 255));
JScrollPane pane = new JScrollPane(table);
getContentPane().add(pane);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object color, boolean isSelected, boolean hasFocus,
int row, int column) {
Color newColor = (Color) color;
table.setBackground(newColor);
if (isSelected) {
table.setBackground(Color.CYAN);
} else {
table.getSelectionBackground();
}
// Discussed in the following section
return null;
}