gpt4 book ai didi

java - CellFactory 与 CellValueFactory

转载 作者:行者123 更新时间:2023-11-29 08:48:23 24 4
gpt4 key购买 nike

让我描述一下情况。

我有一个带有整数变量的类,我有一个包含 3 列“< 100”、“100-200”和“> 200”的 TableView,其中添加了 MyClass 对象。根据对象的 myIntValuex 会显示在正确的列中。现在我想知道获得此结果的最佳方法是什么。

第一个选项,在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com