作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
如何轻松地将自定义(可重用)cellFactories 应用到 javafx.scene.control.TableCell;
或 javafx.scene.control.TableView;
?
最佳答案
要应用自定义 CellFactories 来格式化单元格中显示的文本、颜色等,以下示例可能会有很好的帮助。
假设您有一个 Bean/POJO Person
:
public class Person {
private double levelOfGrowth = 0;
public Person() {};
public Person(double levelOfGrowth) {
this.levelOfGrowth = levelOfGrowth;
};
public double getLevelOfGrowth() {
return levelOfGrowth;
}
}
其中 levelOfGrowth
是一个人的成长已经完成多少的百分比值。
它可能设置在 0.00 到 1.00 之间。
我们还假设您已经在绑定(bind)到 Controller MainWindowController
的 FXML 中创建了 View 。您还设置列以显示 levelOfGrowth
到 id levelOfGrowthColumn
public class MainWindowController implements Initializable {
@FXML
public TableColumn levelOfGrowthColumn;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
ObservableList<Person> persons = FXCollections.observableArrayList();
persons.add(new Person(0));
persons.add(new Person(0.5));
persons.add(new Person(1));
levelOfGrowthColumn.setCellValueFactory(new PropertyValueFactory<Person, Double>("levelOfGrowth"));
}
}
上例中的问题是 View 中显示的值类似于 0.5 而不是 50%。我们还可能希望将 100% 等值的颜色更改为绿色。
我们需要的是一个(可重用的)类,它描述了如何在 View 中格式化/打印 double 值,并将该类与 levelOfGrowthColumn
连接起来。
public class PercantageFormatCell extends TableCell<Object, Double> {
public PercantageFormatCell() {
}
@Override
protected void updateItem(Double item, boolean empty) {
super.updateItem(item, empty);
// If the row is not empty but the Double-value is null,
// we will always display 0%
if (!empty && null == item) {
item = new Double(0.0d);
}
// Here we set the displayed text to anything we want without changing the
// real value behind it. We could also have used switch case or anything you
// like.
setText(item == null ? "" : NumberFormat.getPercentInstance().format(item));
// If the cell is selected, the text will always be white
// (so that it can be read against the blue background),
// if the value is 1 it will be green.
if (item != null) {
double value = item.doubleValue();
if (isFocused() || isSelected() || isPressed()) {
setTextFill(Color.WHITE);
} else if (value < 1) {
setTextFill(Color.BLACK);
} else {
setTextFill(Color.GREEN);
}
}
}
}
public class MainWindowController implements Initializable {
@FXML
public TableColumn levelOfGrowthColumn;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
ObservableList<Person> persons = FXCollections.observableArrayList();
persons.add(new Person(0));
persons.add(new Person(0.5));
persons.add(new Person(1));
// New code START
// In case we have multiple columns with percent-values it
// might come in handy to store our formatter
Callback<TableColumn, TableCell> percantageCellFactory =
new Callback<TableColumn, TableCell>() {
public TableCell call(TableColumn p) {
return new PercantageFormatCell();
}
};
// Now all we have to do is to apply it
levelOfGrowthColumn.setCellFactory(percantageCellFactory);
// New code END
levelOfGrowthColumn.setCellValueFactory(new PropertyValueFactory<Person, Double>("levelOfGrowth"));
}
}
关于java - 如何为 TableView 使用自定义 cellFactories,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12027285/
我是一名优秀的程序员,十分优秀!