gpt4 book ai didi

javafx - 是否可以在 Javafx 中为 Listview 设置两次单元工厂

转载 作者:行者123 更新时间:2023-12-01 12:25:00 25 4
gpt4 key购买 nike

我在 ListView 中设置了一个单元格工厂来更改所选单元格的颜色。另一个用于拖放操作。两者不能同时工作。细胞工厂:1个

listView.setCellFactory( new Callback<ListView<String>, ListCell<String>>()
{
@Override
public ListCell<String> call( ListView<String> param )
{
ListCell<String> listCell = new ListCell<String>()
{
@Override
protected void updateItem( String item, boolean empty )
{
super.updateItem( item, empty );
setText( item );
}
};
Cell factory:2
listView.setCellFactory(list -> {

ListCell<String> cell = new ListCell<String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? null : item);

}
};

最佳答案

setCellFactory 是一个设置方法,它的行为与任何其他设置方法一样:即它设置 ListView 的(唯一的) 的值cellFactory 属性。就像你有这样的代码一样

someObject.setIntValue(5);
someObject.setIntValue(42);

你会期望 someObjectintValue 属性是 42,如果你调用 setCellFactory 两次,细胞工厂将是您传递给它的第二个值。

您所说的最简单的方法是将所有功能简单地组合在一个细胞工厂中:

listView.setCellFactory(list -> {

ListCell<String> cell = new ListCell<String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? "" : item);

// modify color here depending on item...
}
};

cell.setOnDragDetected(e -> { /* ... */ });
// other drag handlers...

return cell ;
});

如果您真的想将事物分离到不同的工厂实现中,您可以使用“装饰器”类型的方法:

public class PlainCellFactory implements Callback<ListView<String, ListCell<String>> {
@Override
public ListCell<String> call(ListView<String> list) {
return new ListCell<String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty) ;
setText(empty ? "" : item);
}
};
}
}

添加颜色:

public class ColorListCellFactory implements Callback<ListView<String>, ListCell<String>> {

private final Callback<ListView<String>, ListCell<String>> originalFactory ;

public ColorListCellFactory(Callback<ListView<String>, ListCell<String>> originalFactory) {
this.originalFactory = originalFactory ;
}

@Override
public ListCell<String> call(ListView<String> list) {
ListCell<String> cell = originalFactory.call(list);
cell.itemProperty().addListener((obs, oldItem, newItem) -> {
// change color depending on newItem (which might be null)...
});
return cell ;
}
}

和拖放:

public class DragAndDropListCellFactory implements Callback<ListView<String>, ListCell<String>> {

private final Callback<ListView<String>, ListCell<String>> originalFactory ;

public DragAndDropListCellFactory(Callback<ListView<String>, ListCell<String>> originalFactory) {
this.originalFactory = originalFactory ;
}

@Override
public ListCell<String> call(ListView<String> list) {
ListCell<String> cell = originalFactory.call(list);
cell.setOnDragDetected(e -> { /* ... */ });
// other drag handlers...
return cell ;
}
}

现在你可以做

PlainCellFactory plainFactory = new PlainCellFactory();
ColorListCellFactory colorFactory = new ColorListCellFactory(plainFactory);
DragAndDropListCellFactory dndFactory = new DragAndDropListCellFactory(colorFactory);
listView.setCellFactory(dndFactory);

关于javafx - 是否可以在 Javafx 中为 Listview 设置两次单元工厂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40758761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com