当我将数据插入jtable i测试时,如果单元格的值为true,我如何更改此单元格的背景颜色
这是我的代码:
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);
}
答案 0 :(得分:0)
尝试添加组件以将其集中起来:
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;
}