[英]javafx tablecolumn cell change
我希望将单元格列设置为接收一个对象圆,以替换来自 observableList 中的数据库的值。 值填充在反映另一列的列中(假设列 A 和 B(从左到右)-它们基本上包含相同的信息,除了-我希望列 B 更改为表示圆对象。这是我的代码到目前为止,如果您有建议,请告知。
status.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList, Circle>, ObservableValue<Circle>>() {
@Override
public ObservableValue<Circle> call(TableColumn.CellDataFeatures<ObservableList, Circle> param) {
String c = (String) param.getValue().get(2); //getting all data in column 2 of the row
System.out.println(c);
switch(c){
case "High":
circle.setFill(Color.GREEN);
healthstatus.setStyle( "-fx-alignment: CENTER;");
break;
case "Medium":
circle.setFill(Color.YELLOW);
healthstatus.setStyle( "-fx-alignment: CENTER;");
break;
case "Low":
circle.setFill(Color.RED);
healthstatus.setStyle( "-fx-alignment: CENTER;");
break;
default:
circle.setFill(Color.BLUEVIOLET);
}
return new SimpleObjectProperty(circle);
}
});
我更喜欢继续使用我拥有的代码,而不必创建一个类来响应设置值。
我已经附上了一张图片来显示我到目前为止的结果。
提前致谢! 图片
更新
正如@kleopatra 指出了之前答案中的一些问题和建议,我已相应地更新了答案。
圆形单元格
用于圆形渲染的自定义 TableCell
public class CircleCell extends TableCell<Person, String> {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (isSafe(item, empty)) {
CellData data = CellData.cellData(item);
Circle circle = new Circle(6);
circle.setFill(data.getColor());
setGraphic(circle);
}
}
private boolean isSafe(String item, boolean empty) {
return !empty && Objects.nonNull(item);
}
}
细胞数据
根据单元格数据保存颜色。
public enum CellData {
HIGH("High", Color.GREEN),
MEDIUM("Medium", Color.YELLOW),
LOW("Low", Color.RED),
NONE("None", Color.BLUEVIOLET);
private String data;
private Color color;
CellData(String data, Color color) {
this.data = data;
this.color = color;
}
public static CellData cellData(String data) {
return Arrays.stream(values())
.filter(e -> e.data.equals(data))
.findAny()
.orElse(NONE);
}
public Color getColor() {
return color;
}
}
设置工厂
使用定制的 CircleCell 设置细胞工厂。
status.setCellFactory(column -> new CircleCell());
简单演示
public class CustomCellDemo extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = new BorderPane();
root.setCenter(getTable());
primaryStage.setScene(new Scene(root, 200, 200));
primaryStage.show();
}
private TableView<Person> getTable() {
TableView<Person> table = new TableView<>();
table.getColumns().add(statusColumn());
table.setItems(getItems());
return table;
}
private TableColumn<Person, String> statusColumn() {
TableColumn<Person, String> status = new TableColumn<>();
status.setCellFactory(col -> new CircleCell());
status.setGraphic(new Label("Status"));
status.setStyle("-fx-alignment:center");
status.setCellValueFactory(new PropertyValueFactory("Status"));
status.setMinWidth(199);
return status;
}
private ObservableList<Person> getItems() {
ObservableList<Person> detail = FXCollections.observableArrayList();
detail.add(new Person("Medium"));
detail.add(new Person("Low"));
detail.add(new Person("High"));
detail.add(new Person(""));
detail.add(new Person(null));
return detail;
}
public class Person {
private SimpleStringProperty status;
public Person(String status) {
this.status = new SimpleStringProperty(status);
}
public String getStatus() {
return status.get();
}
public void setStatus(String status) {
this.status = new SimpleStringProperty(status);
}
}
}
输出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.