2017-10-09 50 views
0

這裏我使用cellfactory創建了一個自定義listview並使用自定義節點進行了更新,但是當我選擇一個單元格時,我需要更改圖形(內容)。我知道我們使用CSS來改變選定單元格的外觀,但在這裏我想更新列表視圖中選定單元格的圖形(內容)不是背景顏色或文本顏色。是否有任何方式可以做到這一點?JavaFX ListView使用cellfactory更新選中的單元格視圖

通常我的列表視圖的層次結構是這樣

Hbox->的Label1,Label2的

但是當我選擇任何細胞應(僅選擇的細胞)更新這樣

Hbox- > Label1,Label2,Labe3,Label4,Button1

這是我的代碼

Callback<ListView<CustomListView>, ListCell<CustomListView>> cellFactory = new Callback<ListView<CustomListView>, ListCell<CustomListView>>() 
{ 

    @Override 
     public ListCell<CustomListView> call(ListView<CustomListView> arg0) 
    { 

     cell = new ListCell<CustomListView>() 
     { 

       @Override 
       protected void updateItem(CustomListView item, boolean bln) 
      { 

       super.updateItem(item, bln); 
          if(item == null) 
          { 
           setGraphic(null); 
           setText(null); 
           return; 
          } 
          else 
       { 
        //Normally my listview will display like this(An HBOX with 2 Labels) 

        HBox h1 =new HBox(); 
        Label itemName=new Label("item1); 
        Label price=new Label("100"); 
        h1.getchildren.addAll(itemName,price); 
        setGraphic(h1); 

        //When i select any cell it should display like this((An Hbox with 4 Labels(selected cell only,remaining cells in first format)) 

        HBox h2 =new HBox(); 
        Label itemName=new Label("item1); 
        Label price=new Label("100"); 
        Label Discount=new Label("50%"); 
        Label Tax=new Label("5%"); 
        Button b1=new Button(); 
        h2.getchildren.addAll(itemName,price,discount,tax,b1); 
        setGraphic(h2); 

        //i have tried with these lines of codes but it does not working properly 
        cell.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> { 
        if(isNowSelected==false)//not selected 
        { 
         //load hbox1 
        } 
        else //selected 
        { 
         //load hbox2 
        } 



       } 
          } 
     }; return cell; 
    } 
}; 
    listView.setCellFactory(cellFactory); 
+0

請閱讀http://stackoverflow.com/help/mcve並採取相應行動:) – kleopatra

回答

0

首先,Cells旨在防止在顯示不同項目時創建不必要的節點創建。因此,每次調用updateItem時都不應該重新創建節點。此外,您絕不會從selected屬性中刪除偵聽器,這意味着可能會有更多的HBox es被更新,這將永遠不會再可見。還有一些在你的代碼相當長的一段的錯誤,防止它編譯...

下面的代碼應該工作,雖然:

listView.setCellFactory(l -> new ListCell<CustomListView >() { 

    // create all nodes that could be displayed 
    private final Label itemName = new Label("item1"); 
    private final Label price = new Label("100"); 
    private final HBox contentContainer = new HBox(); 

    private final Label discount = new Label("50%"); 
    private final Label tax = new Label("5%"); 
    private final Button button = new Button(); 

    { 
     // update HBox every time the selection changes 
     selectedProperty().addListener((observable, oldValue, newValue) -> { 
      CustomListView item = getItem(); 
      if (!isEmpty() && item != null) { 
       updateItemSelection(item, newValue); 
      } 
     }); 
    } 

    @Override 
    protected void updateItem(CustomListView item, boolean empty) { 
     super.updateItem(item, empty); 

     if (empty || item == null) { 
      setGraphic(null); 
     } else { 
      setGraphic(contentContainer); 
      updateItemSelection(item, isSelected()); 
     } 
    } 

    private void updateItemSelection(CustomListView item, boolean selected) { 
     // update for HBox for non-empty cells based on selection 
     if (selected) { 
      contentContainer.getChildren().setAll(itemName, price, discount, tax, button); 
     } else { 
      contentContainer.getChildren().setAll(itemName, price); 
     } 
    } 

}); 
+0

yesss its worked.The錯誤在我的代碼,因爲我只是修改成可以理解的方式。謝謝很多fabian你是我的英雄。你能告訴我hbox.getchildren.addAll和hbox.getchildren.setAll之間的區別,就像你在代碼中提到的那樣。謝謝。 –

+0

這段代碼對於那些在javafx中尋找可擴展列表視圖的人來說非常有用(而不是擴展它將更新選定項目上的內容,例如:通常它只顯示項目名稱,如果選擇了任何單元格,則表示它將顯示itemname,price等。) –

+0

@ihsanikbal'list.setAll(someElements);'與'list.clear();效果相同。 list.addAll(someElements);'但它只觸發一個更新... – fabian

相關問題