- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个表列:
TableColumn<Foo, String> colStatus = new TableColumn("Status");
colStatus.setCellValueFactory(new PropertyValueFactory<>("statusElement"));
在此表上,我想应用此 cellFactory TextFieldTableCell.forTableColumn()
,这将使单元格可编辑。
但我也想将其与自定义 cellFactory 结合起来:
colStatus.setCellFactory(new Callback<>() {
public TableCell<Foo, String> call(TableColumn param) {
return new TableCell<>() {
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (!isEmpty()){
if(item.equals("error")){
this.setTextFill(Color.RED);
setText(item);
}else{
this.setTextFill(Color.Black);
setText(item);
}
}
}
};
}
});
此单元格工厂根据单元格值设置单元格的文本颜色。
但我不知道如何使单元格可编辑,而且还根据值自定义其颜色。
这是 MCVE:
@Override
public void start(Stage primaryStage){
List<String> test = new ArrayList<>();
test.add("done(green)");
test.add("done(green)");
test.add("fail(red)");
test.add("done(green)");
TableView<String> tableView = new TableView<>();
tableView.setColumnResizePolicy(TableView.UNCONSTRAINED_RESIZE_POLICY);
tableView.setEditable(true);
TableColumn<String, String> col = new TableColumn<>("Column");
col.setCellValueFactory(data -> new SimpleStringProperty(data.getValue()));
col.setCellFactory(TextFieldTableCell.forTableColumn());
//I want to apply a color based by value from cell
tableView.getColumns().add(col);
tableView.setItems(FXCollections.observableArrayList(test));
primaryStage.setScene(new Scene(tableView));
primaryStage.show();
}
最佳答案
我终于发现我可以使用 TextFieldTableCell.forTableColumn()
进行自定义。
只需覆盖TextFieldTableCell
。
private static class CustomCell extends TextFieldTableCell<String, String>{
@Override
public void updateItem(String item, boolean empty){
super.updateItem(item, empty);
if(item == null || empty) {
setText(null);
return;
}
if(!isEmpty()){
if(item.equals("error")){
this.setTextFill(Color.RED);
setText(item);
}else{
this.setTextFill(Color.BLACK);
setText(item);
}
}
}
}
我固定的想法是 setCellFactory
将 CallBack
作为参数。
所以我尝试了很多方法来获取返回 TableCell
的 CallBack
在我看到@Sedrick的回答后。
我发现我可以发送 lambda 实现,例如:setCellFactory(e -> new CustomCell())
。
感谢@Sedrick 和@kleopatra。
关于JavaFX - 组合两个 CellFactory - TextFieldTableCell.forTableColumn() 和自定义一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57677244/
我有一个 ReadOnlyBooleanProperty名为 caution对于每个 Trade我创建的对象。每个Trade对象填充 TableView 中的一行 fxTransactionLog .
我有一个奇怪的错误,我找不到合适的解决方案。我想向我的应用程序添加一个显示任务分类的 TreeTableView。由于用户可以自己添加任务,但这些任务必须与已经存在的任务区分开来,我希望它们在我的 T
我创建了一个单元工厂包装器来启用 JavaFX 表格单元的自定义配置。请参阅下面的代码。 package idmas.controller.modules.tableview; import idma
我对 JavaFX 中的 TreeView 有疑问。我不太明白使用细胞工厂的意义。 假设我有以下树: public void initialize(TreeView treeView){ tr
假设我有一个给定的 TableView 。我有一列 LocalDate键入一组cellFactory() 。 birthdayColumn.setCellFactory(column -> {
让我描述一下情况。 我有一个带有整数变量的类,我有一个包含 3 列“ 200”的 TableView,其中添加了 MyClass 对象。根据对象的 myIntValue,x 会显示在正确的列中。现在我
我正在尝试编写一种方法,允许我为作为参数传递的特定列设置列工厂。在这种情况下,我有订单和食品,这两个类在某个时候都显示在 TableView 中,并且都有一个列,我想将其格式化为价格。 它是这样工作的
TL;DR:监听器也在其他单元格上激活。 我有一个 TreeView,其中包含表示自定义类数据的不同 TreeItem。如果基础数据的 BooleanProperty 发生变化,则单元格应更改其颜色。
我有几个用于为约会调度程序应用程序选择日期和时间的ComboBox,但是当我自定义CellFactory时,它只能正确禁用正确的项目第一次单击并展开时。基本上,它应该禁用允许您选择 LocalDate
正如标题所示,我需要通过设置 CellFactory 将图像插入到 TableView 的列中但是通过FileChooser获取路径的图像没有显示在表格上。但是,如果我选择第一行,它会突出显示,就好像
我使用带有 CellFactory 的 ListView 为每个列表条目生成 Labels。当列表条目被选中时,无论我为list-view new ListCell () {
如何轻松地将自定义(可重用)cellFactories 应用到 javafx.scene.control.TableCell; 或 javafx.scene.control.TableView;? 最
我有: public abstract class V {} public class PV extends V {} public class TV extends V {} 我还有: ListVi
我正在使用包含 BillingTableRow 类型项目的 JavaFX TreeTableView。我希望根据 BillingTableRow 中的方法 getType() 给出的值对列进行样式设置
这是 this question 的后续内容... FXML 文件复制如下:
我有一个表列: TableColumn colStatus = new TableColumn("Status"); colStatus.setCellValueFactory(new Propert
我有一个小型概念验证应用程序,其中包含 6 个 Labels、一个 ComboBox 和一个 Button,所有这些都是使用 SceneBuilder 创建的。 通过单击按钮,应用程序会进行 Json
我有一个 ListView点击监听器。我想在悬停时添加新的监听器。我用 CellFactory 编码了那个新的监听器(下面的代码)。仅使用此代码我的 ListView ( ) 显示没有文本的项目,但
ageColumn.setCellFactory(param -> new TableCell(){ @Override protected void updateItem(Strin
看看下面的工作代码: class MyType{ SimpleStringProperty myname; SimpleObjectProperty mycolor
我是一名优秀的程序员,十分优秀!