2015-12-10 50 views
0

我開發了下面列出的代碼,該代碼不起作用,因爲它沒有將行的顏色設置爲黃色,因此從數據庫表中檢索相關聯的時間戳值大於前一行的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的背景顏色。

+0

*「我將只提供代碼的膽量,節省您不必在所有看行「*爲了更好地提供幫助,可以發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 –

+0

Andrew I提供了我認爲最小的必需代碼來幫助解決此問題。提供的代碼是我想要的,因爲需要軟件工程師來提供解決方案。顯而易見的問題是方法highlghtResultRows,它是按照書面提供的。我已經提供了生成此方法的輸入參數的代碼的子集。不是100%確定還需要什麼。 – rgwest61

+0

*「安德魯我提供了我認爲最小的必需代碼來幫助解決此問題」*如果您無法解決問題,則無法判斷是否包含相關代碼。 OTOH我鏈接到一個由已經解決了許多新手問題的人編寫的文檔,並且知道在包含可運行代碼的情況下可以更容易地提供幫助。但是,嘿,這是你的問題,如你所願。我只是投票結束。祝您好運! –

回答

0

在這種情況下,你可以介紹一個方法像下面,你可以通過你的jtable作爲參數:

public void filterRowsWithDifferentColor(JTable table) { 

    table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() { 

     @Override 
     public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { 
      final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 

      if (!isSelected) { 
       try {//assuming the time displaying in your table column 3 with the given format (11:30) you can change it as you need 
        Time timeValue = new Time(new SimpleDateFormat("HH:mm") 
          .parse(table.getValueAt(row, 3).toString()).getTime()); 

        long startTime = timeValue.getTime(); 

        long endTime = System.currentTimeMillis(); 
        long differenceTime = endTime - startTime; 

        if (TimeUnit.MILLISECONDS.toMillis(differenceTime) > 1500) { 

         c.setBackground(Color.YELLOW); 

        } 
       } catch (ParseException ex) { 
        ex.printStackTrace(); 
       } 

      } 
      return c; 
     } 
    }); 

} 
+0

Madushan感謝您的建議代碼。在我的情況下,需要更改比較兩個連續的表格行彼此的時間值,而不是當前時間。如果用戶向前/向下滾動,則工作,但如果用戶以反向/向上方向滾動,我還沒有找到解決方案。如果當前進程行(第n-1行)的時間值至少爲1500毫秒,則無法確定如何將先前處理的行(第n行)單元格的顏色設置爲黃色。先前處理的行不再可用於更新。任何額外的幫助將再次被讚賞。謝謝,羅素 – rgwest61