Wer*_*erb 7 java listview javafx
我正在学习JavaFX,我想创建一个正常工作的单元工厂,直到我想删除我的一行ListView:
plateList.setCellFactory(new Callback<ListView<Car>, ListCell<Car>>() {
@Override
public ListCell<Car> call(ListView<Car> param) {
ListCell<Car> cell = new ListCell<Car>() {
@Override
protected void updateItem(Car item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item.getPlate());
}
}
};
return cell;
}
});
Run Code Online (Sandbox Code Playgroud)
我正在填充ListView一些示例数据:
ObservableList<Car> sample = FXCollections.observableArrayList();
sample.add(new Car("123-abc", "opel", "corsa", 5.5));
sample.add(new Car("123-cba", "vw", "passat", 7.5));
plateList.setItems(sample);
Run Code Online (Sandbox Code Playgroud)
现在我将看到我期望的ListView将是以下内容:
如果删除行ex:第一行(123-abc),ListView将如下所示:
这是删除部分:
@FXML
private void deleteBtnAction() {
plateList.getItems().remove(plateList.getSelectionModel().getSelectedItem());
ObservableList<Car> t = plateList.getItems();
plateList.setItems(t);
}
Run Code Online (Sandbox Code Playgroud)
如果我删除了单元工厂,程序按预期工作.任何帮助是极大的赞赏.
Ada*_*dam 14
尝试更改为以下内容,这是必需的,因为JavaFX重用了列表单元格,因此updateItem()在传递null时也需要清空未使用的列表单元格
super.updateItem(item, empty);
if (item != null) {
setText(item.getPlate());
} else {
setText(""); // <== clear the now empty cell.
}
Run Code Online (Sandbox Code Playgroud)
完整的SSCCE
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;
public class JavaFxListView extends Application {
private static class Car {
private String plate;
public Car(String plate, String string2, String string3, double d) {
this.plate = plate;
}
public String getPlate() {
return plate;
}
}
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage arg0) throws Exception {
ListView<Car> plateList = new ListView<Car>();
plateList.setCellFactory(new Callback<ListView<Car>, ListCell<Car>>() {
@Override
public ListCell<Car> call(ListView<Car> param) {
ListCell<Car> cell = new ListCell<Car>() {
@Override
protected void updateItem(Car item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item.getPlate());
} else {
setText("");
}
}
};
return cell;
}
});
Button delete = new Button("Delete");
ObservableList<Car> sample = FXCollections.observableArrayList();
sample.add(new Car("123-abc", "opel", "corsa", 5.5));
sample.add(new Car("123-cba", "vw", "passat", 7.5));
delete.setOnAction((e) -> {
plateList.getItems().remove(plateList.getSelectionModel().getSelectedItem());
ObservableList<Car> t = plateList.getItems();
plateList.setItems(t);
});
plateList.setItems(sample);
arg0.setScene(new Scene(new VBox(plateList, delete)));
arg0.show();
}
}
Run Code Online (Sandbox Code Playgroud)
根据Cell updateItem方法的 Java 文档,推荐用法与接受的用法略有不同:
protected void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
setGraphic(null);
} else {
setText(item.toString());
}
}
Run Code Online (Sandbox Code Playgroud)
区别在于参数的使用empty。但@Adam 的解决方案在主要情况下也应该可以正常工作。
| 归档时间: |
|
| 查看次数: |
12794 次 |
| 最近记录: |