我開發了下面列出的代碼,該代碼不起作用,因爲它沒有將行的顏色設置爲黃色,因此從數據庫表中檢索相關聯的時間戳值大於前一行的1.5秒(1500毫秒)。我將只提供代碼的膽量,節省您不必看都行,如調試輸出,記錄異常等將JTable行設置爲不同顏色的代碼無法正常工作
private void processQueryResults(ResultSet results){
Vector<Vector<String>> resultRows = new Vector<Vector<String>>();
Vector<String> resultRowVector = null;
Vector<Timestamp> timestampVectors = null;
resultRows.clear();
if (results != null){
timestampVectors = new Vector<Timestamp>();
While (results.next()){
resultRowVector = new Vector<String>();
timestampVectors.add(results.getTimestamp("timestamp_column");
resultRowVector.add(results.getString("columnA");
resultRowVector.add(results.getString("columnB");
.....
.....
resultRows.add(resultRowVector);
}
JTable displayTable =
highlightResultRows(new DefaultTableModel(resultRows, colHdrs),
timestampVectors);
displayTable.setPreferredScrollableViewportSize(new Dimension(900,500));
......
......
}
private JTable highlightResultRows(DefaultTableModel model,
final Vector<Timestamp> timestamps){
Timestamp previousTimestamp = null;
JTable highlightedTable = new JTable(model){
private static final long serialVersionUID = 1L;
public Component prepareRenderer(TableCellRenderer renderer, int row, int col){
Component comp = super.prepareRenderer(renderer, row, col);
if (previousTimestamp != null){
if (previousTimestamp.getTime() < (timestamps.get(row).getTime() - 1500)){
comp.setBackground(Color.YELLOW);
}
else
{
comp.setBackground(Color.BLUE);
}
}
else
{
comp.setBackground(Color.BLUE);
}
previousTimestamp = timestamps.get(row);
System.out.println("previousTimestamp: [" + previousTimestamp.getTime() + "]");
return comp;
}
};
return highlightedTable;
}
我所知道的是沒有發生的是prepareRenderer中的邏輯()方法不執行,因爲用於顯示previousTimestamp設置爲什麼的卸載System.out.println不會被輸出。同樣所有的行都被設置爲BLUE的背景顏色。
*「我將只提供代碼的膽量,節省您不必在所有看行「*爲了更好地提供幫助,可以發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 –
Andrew I提供了我認爲最小的必需代碼來幫助解決此問題。提供的代碼是我想要的,因爲需要軟件工程師來提供解決方案。顯而易見的問題是方法highlghtResultRows,它是按照書面提供的。我已經提供了生成此方法的輸入參數的代碼的子集。不是100%確定還需要什麼。 – rgwest61
*「安德魯我提供了我認爲最小的必需代碼來幫助解決此問題」*如果您無法解決問題,則無法判斷是否包含相關代碼。 OTOH我鏈接到一個由已經解決了許多新手問題的人編寫的文檔,並且知道在包含可運行代碼的情況下可以更容易地提供幫助。但是,嘿,這是你的問題,如你所願。我只是投票結束。祝您好運! –