为什么此代码无法正常运行?这个JTable中的所有项目都有RED COLOR? 我只调用了类 MonCellRenderer 一次,但它循环很多次。我该怎么办?
我有一个叫jtable的课程:
public void Tampil(){
DataTabel.setNumRows(0);
try {
String thn ;
String bln ;
String btc ;
DataTabel.setNumRows(0);
QueryManajemen q = new QueryManajemen();
String[] kolom = new String[DataTabel.getColumnCount()];
for (int a = 0; a < DataTabel.getColumnCount(); a++) {
kolom[a] = DataTabel.getColumnName(a);
}
String[][] data = //some code;
String[][] notClean = //some code;
for (int j = 0; j < notClean.length; j++) {
listSearch.add(notClean[j][0].toString());
}
**tblBatch.setDefaultRenderer(Object.class, new MonCellRenderer());**
} catch (SQLException ex) {
}
}
}
然后打电话给一个班级:
private List<String> listSearch = new ArrayList<String>();
public List<String> getListSearch() {
return listSearch;
}
public class MonCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value,isSelected, hasFocus, row, column);
if (column == 0 || listSearch.contains(value)) {
setBackground(Color.RED);
} else {
setBackground(Color.WHITE);
}
return this;
}
}