這裏我使用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);
請閱讀http://stackoverflow.com/help/mcve並採取相應行動:) – kleopatra