- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我必须实现大量自定义 TableCell,其行为依赖于模型的更改。我可以设法以某种方式获得预期的结果,但我认为在许多情况下这是一种变通方法,而不是一个非常好的解决方案。我已使用绑定(bind)/监听器来实现预期结果,但我面临的问题是我可能会多次添加监听器/绑定(bind)属性,这会造成内存泄漏。
这是我的意思的一个例子。
Controller :
public class Controller implements Initializable {
@FXML private TableView<Model> table;
@FXML private TableColumn<Model, String> column;
@FXML private Button change;
@Override
public void initialize(URL location, ResourceBundle resources) {
column.setCellValueFactory(data -> data.getValue().text);
column.setCellFactory(cell -> new ColoredTextCell());
Model apple = new Model("Apple", "#8db600");
table.getItems().add(apple);
table.getItems().add(new Model("Banana", "#ffe135"));
change.setOnAction(event -> apple.color.setValue("#ff0800"));
}
@Getter
private class Model {
StringProperty text;
StringProperty color;
private Model(String text, String color) {
this.text = new SimpleStringProperty(text);
this.color = new SimpleStringProperty(color);
}
}
private class ColoredTextCell extends TableCell<Model, String> {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty || getTableRow() == null || getTableRow().getItem() == null) {
setGraphic(null);
return;
}
Model model = (Model) getTableRow().getItem();
Text text = new Text(item);
text.setFill(Color.web(model.getColor().getValue()));
// This way I add the listener evey item updateItem is called.
model.getColor().addListener((observable, oldValue, newValue) -> {
if (newValue != null) {
text.setFill(Color.web(newValue));
} else {
text.setFill(Color.BLACK);
}
});
setGraphic(text);
}
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Button?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="stackoverflow.tabpane.Controller">
<VBox>
<Button fx:id="change" text="Change color"/>
<TableView fx:id="table">
<columns>
<TableColumn fx:id="column" prefWidth="200"/>
</columns>
</TableView>
</VBox>
</AnchorPane>
由于单元格不能直接观察到颜色属性,因此如果它发生变化,则不会调用 updateItem,因此我必须以某种方式进行监听。我需要在 color 更改后触发 updateItem
。这将导致对监听器内容的单次调用。
有没有办法在同一个单元格中监听模型的另一个变化,或者以某种方式调用更新项,以便呈现变化。
最佳答案
使用监听器和绑定(bind)不会导致任何问题,只要您记得在不再需要时删除它们即可。为了让它更安全,你应该使用弱监听器(绑定(bind)使用弱监听器)。当您想根据行项目的不同属性更改单元格文本的颜色时,我认为使用绑定(bind)会更容易。请注意,TableCell
继承自 Labeled
,这意味着它具有 textFill
属性;无需创建 Text
节点来更改文本的颜色。
这是一个例子:
import javafx.beans.binding.Bindings;
import javafx.scene.control.TableCell;
import javafx.scene.paint.Color;
public class ColoredTextCell extends TableCell<Model, String> {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
/*
* I was getting a NullPointerException without the "getTableRow() == null"
* check. I find it strange that a TableCell's "updateItem" method would be
* invoked before it was part of a TableRow... but the added null check seems
* to solve the problem (at least when only having two items in the table and
* no scrolling).
*/
if (empty || item == null || getTableRow() == null) {
setText(null);
textFillProperty().unbind();
} else {
setText(item);
Model rowItem = getTableRow().getItem();
textFillProperty().bind(Bindings.createObjectBinding(
() -> Color.valueOf(rowItem.getColor()),
rowItem.colorProperty()
));
}
}
}
调用 textFillProperty().unbind()
将防止内存泄漏。并且在绑定(bind)属性时,先前的绑定(bind)(如果有)将被删除。如果你真的很偏执,你也可以在 bind(...)
之前调用 unbind()
。如果您真的,真的偏执,那么您可以将 ObjectBinding
存储在一个字段中并调用 dispose()
在适当的时候(甚至取消它)。
关于JavaFx:如何正确触发 TableCell 中的 updateItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56376551/
我正在尝试在我的应用程序中使用 NGXS 状态运算符,但我无法找到如何使用它们进行稍微复杂的更新的好示例。 例如,NGXS 的文档显示了更新此状态的示例: @State({ name: 'anim
我正在尝试更新具有许多属性(超过 100 个)的项目。我可以看到有两种方法可以更新此项目。 PutItem :PutItem 可以更新项目,根据我的理解,我需要为每个请求发送所有 100 个属性,因为
我尝试将图像添加到 TabelView 中的单元格。我读了一些解决方案(例如: Placing an image into a JavaFX 2 table ),它们都必须覆盖方法“updateIte
我正在尝试制作一个包含大量项目(超过 10000)的组合框。它初始化没有问题。但当我点击它时,它就卡住了。为了调试,我创建了自己的 listCell 并遵循 updateitem 函数。当我点击时,它
我试图在 ListView 中创建自定义单元格,但每次添加新项目时,updateItem(TextFlow item, Boolean empty) 都会执行两次:一次接收 null 和 true,第
我有一个 JAVAFX 程序,其中包含新闻列表。我根据每个新内容的严重性为其着色,并将其添加到 ListView 中。 当我向列表中添加新内容时,我收到 NullPointerException。 调
我正在使用 ListView在 JavaFX 应用程序中。列表中的项目需要的不仅仅是一个字符串来显示它们,所以我做了一个自定义实现 ListCell ,其中 T是我正在显示的对象的类。在这个自定义类中
我们可以仅使用全局二级索引更新 dynamodb 项目吗? $response = $dynamodbClient->updateItem(array( 'TableName'
默认情况下,如果具有指定索引的对象不存在,DynamoDB 将创建一个新条目。有没有办法阻止这种情况发生?我可以在更新之前查询表中的 key ,但最好在一个请求中完成所有这一切。 最佳答案 使用条件表
如何将项目附加到 dynamodb 中的现有 map 我想添加一组新的位置/名称/标签。 dynamodb 中的结构如下所示 { "user": "xyz", "itemdetails": [
我希望 listView 中的项目全部大写并有换行符。 我使用 setCellFactory 来提供 Cell.updateItem 的实现。这很好用。当我将 ListView 中换行的解决方法添加到
如何将项目附加到 dynamodb 中的现有 map 我想添加一组新的位置/名称/标签。 dynamodb 中的结构如下所示 { "user": "xyz", "itemdetails": [
我正在尝试将图像添加到 tableView,并经历了几个问题和答案,但它仍然不起作用。 tableView 中的其他字段(如名称)已正确加载。 Intelij 告诉我 updateItem 从未使用过
我必须实现大量自定义 TableCell,其行为依赖于模型的更改。我可以设法以某种方式获得预期的结果,但我认为在许多情况下这是一种变通方法,而不是一个非常好的解决方案。我已使用绑定(bind)/监听器
我正在尝试从我的 DynamoDB 表中的 ITEM 中删除 LIST 元素。我相信我的设置正确,但似乎无法传递整数。 var params = { TableName: DB_TABLE_NAM
有没有办法用updateItem实现以下几点: 如果 DynamoDB 中不存在属性,则添加属性 如果属性存在于 DynamoDB 中,则更新属性 如果这些属性不包含在参数中,则保持这些属性不变。 这
我正在运行用 Node.js 编写的 AWS Lambda 服务,该服务与 DynamoDB 数据库交互。我的一种方法在 DynamoDB 上执行更新 (AWS.DynamoDB.DocumentCl
我从 tableView 获取行。我使用 setRowFactory 来获取行,后来我使用它。 我需要重写 updateItem 方法来获取每一行,如果该行内的项目有错误,则使用红色,否则使用绿色。
尝试创建 Lambda 以从 Kinesis 流更新 DynamoDB。这是我的更新声明: var response = dd.updateItem({ 'Key': {'S'
TableCell 中的 updateItem() 方法何时被调用?是在与该单元格关联的属性发生更改时吗? 在我的应用程序中,我有一个线程,根据提供的超链接下载内容。我有一个 TableView,在两
我是一名优秀的程序员,十分优秀!