【发布时间】:2020-10-03 16:15:59
【问题描述】:
我认为可以根据以下 JavaFX 11 文档项有条件地格式化 JavaFX ComboBoxTableCell 标签:
默认情况下,
ComboBoxTableCell在未被编辑时呈现为Label[...]。
-
Labeled类
setTextFill方法说明:
设置属性
textFill的值。
如果我的假设是正确的,我想知道如何修改以下 SSCCE,以便将任何 ComboBoxTableCell 标签 textFill 属性设置为以下 Paint 值。谢谢!
-
Color.Red如果其值无效
-
Color.Blue如果它的值是为不同的行设置的。
public class PersonInRoom extends Application
{
private final ObservableList<Person> data = FXCollections.observableArrayList(
new Person("Jacob" , "Kitchen"),
new Person("Isabella", "Bedroom"),
new Person("Ethan" , "Attic"));
TableView<Person> table ;
public static void main(String[] args) {launch(args);}
@Override
public void start(Stage stage)
{
TableColumn nameCol = new TableColumn("Name");
nameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("name"));
TableColumn roomCol = new TableColumn("Room");
roomCol.setCellValueFactory(new PropertyValueFactory<Person, String>("room"));
roomCol.setCellFactory(ComboBoxTableCell.forTableColumn("Bathroom", "Bedroom", "Kitchen"));
roomCol.setOnEditCommit((EventHandler<CellEditEvent<Person, String>>) t -> t.getTableView().getItems().get(t.getTablePosition().getRow()).setRoom(t.getNewValue()));
table = new TableView<>();
table.setEditable(true);
table.setItems(data);
table.getColumns().addAll(nameCol, roomCol);
VBox vbox = new VBox();
vbox.getChildren().addAll(table);
Scene scene = new Scene(vbox);
stage.setScene(scene);
stage.show();
}
public static class Person
{
private final SimpleStringProperty name;
private final SimpleStringProperty room;
private Person(String name, String room)
{
this.name = new SimpleStringProperty(name);
this.room = new SimpleStringProperty(room);
}
public String getName() {return name.get();}
public String getRoom() {return room.get();}
public void setRoom(String r) {room.set(r);}
}
}
【问题讨论】:
-
您需要一个自定义单元格并覆盖 updateItem 以根据上下文设置颜色
-
@kleopatra,非常感谢您的评论。我已经在我自己的问题的答案中包含了根据您的指导更新的 SSCCE 代码 sn-p。
标签: javafx combobox format label