让我描述一下情况。
我有一个带有整数变量的类,还有一个包含3列'< 100‘、'100-200’和‘class 200’>列的TableView,其中添加了MyClass对象.根据对象的myIntValue,x显示在正确的列中。现在我想知道得到这个结果最好的方法是什么。
First option,在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;
}
}在我的控制器里:
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;
}
}在我的控制器里:
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;
}
};
}我能够让这两个选项都发挥作用,但我想知道其中一个选项是否是更好的方法(例如,为了更好的性能,.)。
发布于 2014-06-02 08:05:13
快速回答:这里很少有计算。因此,无论是从内存性能还是CPU性能来看,无论您是以这种方式还是以另一种方式进行,都不重要。与显示TableView时发生的其他一切相比,任何差异都是可以忽略的。
所以,在我看来,这纯粹是一个设计问题。从域的角度来看,MyClass更重要的特性/属性是什么?它是myIntValue,还是myIntValue可以归入的三个类别?
根据我现在对这个问题的理解,我会选择选项二,因为我将较少/介于/更多的选项视为可视化细节,而不是一个重要的领域方面。但你可以选择不这样做,这样做没有任何问题。:-)
https://stackoverflow.com/questions/23970333
复制相似问题