- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一种方法,允许我为作为参数传递的特定列设置列工厂。在这种情况下,我有订单和食品,这两个类在某个时候都显示在 TableView 中,并且都有一个列,我想将其格式化为价格。
它是这样工作的:
priceColumn.setCellFactory(col ->
new TableCell<Food, Double>() {
@Override
public void updateItem(Double price, boolean empty) {
super.updateItem(price, empty);
if (empty) {
setText(null);
} else {
setText(String.format("%.2f €", price));
}
}
}
);
这是我的 Formatting 类,我在其中尝试使其通用,而不是为每一列复制粘贴相同的内容。问题是它不会显示任何内容。
public static <T> void priceCellFormatting(TableColumn tableColumn){
System.out.println();
tableColumn.setCellFactory(col ->
new TableCell<T, Double>() {
protected void updateItem(double item, boolean empty) {
super.updateItem(item, empty);
if(empty){
setText(null);
}else {
setText(String.format("%.2f €", item));
}
}
});
}
我调用这个方法,除了价格之外的每一列都会被填充:
private void fillTableListView() {
nameColumn.setCellValueFactory(new PropertyValueFactory<Order, String>("name"));
amountColumn.setCellValueFactory(new PropertyValueFactory<Order, Integer>("amount"));
priceColumn.setCellValueFactory(new PropertyValueFactory<Order, Double>("price"));
totalColumn.setCellValueFactory(new PropertyValueFactory<Order, Double>("total"));
Formatting.priceCellFormatting(priceColumn);
try {
orderTableView.setItems(OrderDAO.getOrder());
} catch (SQLException e) {
System.out.println("Exception at filling tablelistview: " + e);
}
}
最佳答案
有一个小错字对您的代码产生了巨大影响。你用过
protected void updateItem(double item, boolean empty)
代替
protected void updateItem(Double item, boolean empty)
由于您使用原始类型 double
而不是也用作类型参数的 Double
类型,因此您不会覆盖 updateItem
方法,而是创建一个新方法。从未使用过此方法。而是使用默认的 updateItem
方法。此实现不会修改单元格的文本。
提示:始终在覆盖方法时使用@Override
注释。这允许编译器检查这样的错误。此外,您还应该在 priceCellFormatting
方法中为方法参数添加类型参数:
public static <T> void priceCellFormatting(TableColumn<T, Double> tableColumn){
System.out.println();
tableColumn.setCellFactory(col ->
new TableCell<T, Double>() {
@Override
protected void updateItem(Double item, boolean empty) {
super.updateItem(item, empty);
if(empty){
setText(null);
}else {
setText(String.format("%.2f €", item));
}
}
});
}
关于JavaFX 使 cellFactory 通用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44470411/
我有一个 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
我是一名优秀的程序员,十分优秀!