为什么 CellFactory 会在这个列表中添加这么多空元素?我明确设置了一个只有“a”和“b”的可观察数组
我不认为绑定有问题......有什么建议吗?
package at.kingcastle.misc;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class MainSpielwiese extends Application {
@Override
public void start(Stage primaryStage) {
ListView<String> lv = new ListView<>();
lv.setItems(FXCollections.observableArrayList(new String[] {"a", "b"}));
StackPane root = new StackPane();
root.getChildren().add(lv);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
lv.setCellFactory(list -> {
ListCell<String> cell = new ListCell<>();
ContextMenu contextMenu = new ContextMenu();
cell.textProperty().bind(Bindings.format("%s", cell.itemProperty()));
return cell;
});
}
public static void main(String[] args) {
launch(args);
}
}