javafx 表列单元格更改
javafx tablecolumn cell change
我希望将单元格列设置为接收对象圆圈,以替换来自 observableList 中的数据库的值。值填充在反映另一列的那一列中(让我们说 col A 和 B(从左到右) - 它们基本上包含相同的信息,除了 - 我希望 col B 改变以表示圆 object.Here 是我的代码至此,有什么建议请指教。
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);
}
});
我更愿意继续使用我的代码,而不必创建 class 来响应设置值。
我附上了一张图片来展示我目前的结果。
提前致谢!
Image
Update
由于@kleopatra 指出了之前回答中的一些问题和建议,我已经相应地更新了答案。
CircleCell
用于圆形渲染的自定义 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 设置 cell-factory。
status.setCellFactory(column -> new CircleCell());
Simple Demo
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);
}
}
}
输出
我希望将单元格列设置为接收对象圆圈,以替换来自 observableList 中的数据库的值。值填充在反映另一列的那一列中(让我们说 col A 和 B(从左到右) - 它们基本上包含相同的信息,除了 - 我希望 col B 改变以表示圆 object.Here 是我的代码至此,有什么建议请指教。
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);
}
});
我更愿意继续使用我的代码,而不必创建 class 来响应设置值。
我附上了一张图片来展示我目前的结果。
提前致谢! Image
Update
由于@kleopatra 指出了之前回答中的一些问题和建议,我已经相应地更新了答案。
CircleCell
用于圆形渲染的自定义 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 设置 cell-factory。
status.setCellFactory(column -> new CircleCell());
Simple Demo
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);
}
}
}
输出