- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
让我描述一下情况。
我有一个带有整数变量的类,我有一个包含 3 列“< 100”、“100-200”和“> 200”的 TableView,其中添加了 MyClass
对象。根据对象的 myIntValue
,x 会显示在正确的列中。现在我想知道获得此结果的最佳方法是什么。
第一个选项,在 MyClass
中创建方法并将它们用作我的列的 CellValueFactory
:
public class MyClass{
...
private int myIntValue;
...
public boolean getLessThan100(){
return myIntValue < 100;
}
public boolean getBetween100And200(){
return myIntValue >= 100 && myIntValue <= 200;
}
public boolean getMoreThan200(){
return myIntValue > 200;
}
}
在我的 Controller 中:
tcLessThan100.setCellValueFactory(new PropertyValueFactory<MyClass, Boolean>("lessThan100"));
tcBetween100And200.setCellValueFactory(new PropertyValueFactory<MyClass, Boolean>("between100And200"));
tcMoreThan200.setCellValueFactory(new PropertyValueFactory<MyClass, Boolean>("moreThan200"));
第二个选项,不要在MyClass
中创建额外的方法,而是使用CellFactory
:
public class MyClass{
...
private int myIntValue;
...
public int getMyIntValue(){
return myIntValue;
}
}
在我的 Controller 中:
tcLessThan100.setCellValueFactory(new PropertyValueFactory<MyClass, Integer>("myIntValue"));
tcLessThan100.setCellFactory(CustomCellFactories.getXCellFactory100());
tcBetween100And200.setCellValueFactory(new PropertyValueFactory<MyClass, Integer>("myIntValue"));
tcBetween100And200.setCellFactory(...);
tcMoreThan200.setCellValueFactory(new PropertyValueFactory<MyClass, Integer>("myIntValue"));
tcMoreThan200.setCellFactory(...);
在 CustomCellFactories 类中:
public static Callback<TableColumn<MyClass, Integer>, TableCell<MyClass, Integer>> getXCellFactory100() {
return new Callback<TableColumn<MyClass, Integer>, TableCell<MyClass, Integer>>() {
@Override
public TableCell<MyClass, Integer> call(TableColumn<MyClass, Integer> param) {
TableCell<MyClass, Integer> cell = new TableCell<MyClass, Integer>() {
@Override
public void updateItem(final Integer item, final boolean empty) {
if (empty) {
setText(null);
setGraphic(null);
} else {
Label label = new Label();
if (item < 100) {
label.setText("x");
}
setGraphic(label);
}
}
};
cell.setEditable(false);
cell.setAlignment(Pos.CENTER);
return cell;
}
};
}
我能够让这两个选项都起作用,但我想知道其中一个选项是否是更好的方法(例如,为了更好的性能,...)。
最佳答案
快速回答:这里进行的计算很少。因此,就内存和 CPU 性能而言,无论采用哪种方式都无关紧要。与显示 TableView
时发生的所有其他事情相比,任何差异都可以忽略不计。
所以,在我看来,这纯粹是一个设计问题。从域的角度来看,MyClass
有哪些更重要的特性/属性?是 myIntValue
还是 myIntValue
可以归入的三个类别?`
根据我现在对问题的理解,我会选择选项二,因为我将小于/介于/大于更多视为可视化细节,而不是重要的领域方面。但是你可以选择不这样做,没有任何问题。 :-)
关于java - CellFactory 与 CellValueFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23970333/
我遇到了一个奇怪的问题,我似乎无法解决。我正在制作一个表格 View ,其中一列内部有一个复选框。 虽然我正在尝试向 CheckBoxTableCell 添加更改监听器,但没有任何运气。 如果我这样做
我有一个填充了自定义对象的TableView;该表显示该对象的属性。 我的问题是如何根据该行的另一个属性的值将不同的属性绑定(bind)到该列? 例如,假设我有这个对象: public class M
让我描述一下情况。 我有一个带有整数变量的类,我有一个包含 3 列“ 200”的 TableView,其中添加了 MyClass 对象。根据对象的 myIntValue,x 会显示在正确的列中。现在我
我想了解这些方法cellValueFactory();和cellFactory(); 他们何时被调用? 它们的目的是什么? 它们如何工作? 最佳答案 这两个工厂都被TableView(或更确切地说是S
错误:类型不匹配:无法从 SimpleStringProperty 转换至ObservableValue . 我正在尝试创建一个带有管理各种数据类型的列的 TreeTableView。这意味着每行可以
我有一个 TableView,其中有一列需要显示我的数据对象中的两个值之一。 在下面的 MCVE 中,我有一个 Person 对象,它可能有也可能没有 nickname 属性。此 nickname 可
我一直在尝试构建一个树表并在 FXML 中定义 cellValueFactories。 当我尝试这个时,我在堆栈跟踪中收到以下错误 Exception in thread "JavaFX Applic
我有一个 JavaFX 表列,我想显示以逗号分隔的字符串列表,除非文本不适合单元格的当前边界,此时它将显示,例如“Foo 和其他 3 个...”,或“3 Bars”,即反射(reflect)列表中元素
如何使用列表从 javafx 中的 CellValueFactory 填充 TableColumns 单元格?我有一个想要在我的列中显示的特定整数列表,但我不知道如何正确使用 setCellValue
我有 javafx 表,其中包括组织的成员明智的每月费用。在此表中,我显示了一些费用及其默认金额,以便在任何情况下用户都可以更新金额或按原样继续。我真正需要的是,如果用户更新任何值,这些值应立即绑定(
我是一名优秀的程序员,十分优秀!