2017-10-09 9 views
0

여기 cellfactory를 사용하여 사용자 지정 목록 뷰를 만들고 사용자 지정 노드로 업데이트했지만 셀을 선택할 때 그래픽 (내용)을 변경해야합니다. 내가 선택한 셀의 모양을 변경하려면 CSS를 사용하지만, 여기에 내가 목록보기에서 선택된 셀의 그래픽 (내용)을 업데이 트하고 싶습니다 배경 색상이나 텍스트 색상이 없습니다.Is 거기에 어떤 식 으로든 어떻게 할거야 ??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

우선 Cell은 다른 항목이 표시 될 때 불필요한 노드 생성을 방지하도록 설계되었습니다. 이러한 이유로 노드 updateItem이 호출 될 때마다 노드를 다시 생성하면 안됩니다. 또한 selected 속성에서 수신기를 제거하지 않으므로 다시 표시되지 않는 HBox 개가 업데이트 될 수 있음을 의미합니다.

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. 내가 이해할 수있는 방식으로 수정되었으므로 코드에 오류가 있습니다. 많은 영감을 주신 Fabian에게 감사드립니다. hbox.getchildren.addAll과 hbox.getchildren.setAll 사이의 차이점을 코드에서 언급 한대로 알려주시겠습니까? 고맙습니다. –

+0

그리고이 코드는 javafx에서 확장 가능한 listview를 보는 사람들에게 매우 유용 할 것입니다. (예를 들어, 확장하는 대신에 select 항목의 내용을 업데이트 할 것입니다. 예 : 어떤 셀이라도 선택하면 일반적으로 항목 이름 만 표시됩니다. itemname, price 등) –

+0

@hsanikbal'list.setAll (someElements);는'list.clear();와 동일한 효과를가집니다. list.addAll (someElements);'단 하나의 업데이트를 트리거합니다 ... – fabian