2014-12-03 61 views
3

我有一個名爲TransactionWrapper的類,我正在使用該類在我的應用程序中填充TableView的ObservableList。這個包裝有一個屬性(枚舉),指出它是一個撤回還是存款。我需要去渲染/格式化數量單元格(根據事務的性質以紅色或綠色顯示它),並且我沒有發現任何可以幫助我進行這場戰鬥的東西。JavaFX TableView:基於行中其他值的格式化一個單元格

基本上我想要做的就是看行,並說如果類型是撤回,顏色文本紅色,如果它是存款的顏色它綠色...我希望有人能幫助我這個。我會在我嘗試使用setCellFactory的時候發佈,正如我在其他地方發現的那樣。這種方法允許我格式化單元格以及它是如何顯示的,但問題在於updateItem函數內部,我可以獲取我的交易類型的值。

amountCol.setCellFactory(new Callback<TableColumn<TransactionWrapper, String>, TableCell<TransactionWrapper, String>>() 
{ 
    @Override 
    public TableCell<TransactionWrapper, String> call(
      TableColumn<TransactionWrapper, String> param) 
    { 
     return new TableCell<TransactionWrapper, String>() 
     { 
      @Override 
      protected void updateItem(String item, boolean empty) 
      { 
       if (!empty) 
       { 
        // should be something like (transaction.getType().equals(TransactionTypes.DEPOSIT) ? true : false;) 
        boolean isDeposit = true; 
        setText(item);       
        if(isDeposit) // should be if type is deposit 
        { 
         setTextFill(Color.GREEN); 
        } 
        else        
        { 
         setTextFill(Color.RED); 
        } 
       } 
      } 
     }; 
    } 
}); 

這裏就是我如何設置我的專欄:

amountCol.setCellValueFactory(cellData -> cellData.getValue().getAmountString()); 

運行關閉稱爲TransactionWrapper與FOL對象:

private final StringProperty transactionTypeString; 
private final StringProperty dateString; 
private final StringProperty amountString; 
private final StringProperty payeeString; 
private final StringProperty categoryString; 
private final StringProperty notesString; 
private Transaction transaction; 

在這個任何想法非常感謝。 :d

感謝, 喬恩

+1

嘗試'getTableRow()。getItem()。getDeposit()'。 – 2014-12-03 22:38:10

回答

7

想通了!感謝詹姆斯的想法,但我採取了一些不同的方式。下面是任何人在未來的閱讀這篇文章的代碼:

amountCol.setCellFactory(new Callback<TableColumn<TransactionWrapper, String>, 
      TableCell<TransactionWrapper, String>>() 
      { 
       @Override 
       public TableCell<TransactionWrapper, String> call(
         TableColumn<TransactionWrapper, String> param) 
       { 
        return new TableCell<TransactionWrapper, String>() 
        { 
         @Override 
         protected void updateItem(String item, boolean empty) 
         { 
          if (!empty) 
          { 
           int currentIndex = indexProperty() 
             .getValue() < 0 ? 0 
             : indexProperty().getValue(); 
           TransactionTypes type = param 
             .getTableView().getItems() 
             .get(currentIndex).getTransaction() 
             .getTransactionType(); 
           if (type.equals(TransactionTypes.DEPOSIT)) 
           { 
            setTextFill(Color.GREEN); 
            setText("+ " + item); 
           } else 
           { 
            setTextFill(Color.RED); 
            setText("- " + item); 
           } 
          } 
         } 
        }; 
       } 
      }); 

的param.getTableView()getItems()獲取(CURRENTINDEX)是關鍵..只好鑽到父位存在,但它。完成了工作。那裏最大的挑戰是找到索引。當我發現indexProperty()函數存在時感覺有點傻..大聲笑。沒有想到要查看可用的課程級功能。快樂的編碼!

+0

不錯的一個!你知道如何設置單元格的樣式,當它被選中時,textfill的顏色看起來不錯 – 2016-08-19 11:24:52

相關問題