tay*_*low 1 java javafx tableview javafx-2
所以我有一个正常工作的表,并从ObservableList这里的代码中获取数据:
public void setMainTableData(ObservableList<FileMP3> list)
{
artistCol.setCellValueFactory(new PropertyValueFactory<FileMP3, String>("artist"));
albumCol.setCellValueFactory(new PropertyValueFactory<FileMP3, String>("album"));
titleCol.setCellValueFactory(new PropertyValueFactory<FileMP3, String>("title"));
trackCol.setCellValueFactory(new PropertyValueFactory<FileMP3, String>("track"));
yearCol.setCellValueFactory(new PropertyValueFactory<FileMP3, String>("year"));
mainTable.setItems(list);
}
Run Code Online (Sandbox Code Playgroud)
然而,这些列并不都包含字符串数据 - 我需要能够插入一个int,可能还有其他类型,如Duration. 的track和year条目存储为整数,且有一个(未示出)入口称为length。这FileMP3作为 a存储在我的对象中Duration,在将其插入表中之前,我没有看到任何明显的方法来操作存储在那里的数据。我希望能够使用Duration.getMillis()然后对其进行一些数学运算以将其转换为可显示的int格式,但我想将其存储在FileMP3as 中Duration。
我在该主题上阅读的所有教程都使用构造函数:
new PropertyValueFactory<FileMP3, String>("genre")
总而言之,我希望能够String在表格中插入除 a 以外的内容。
您可以将 String 替换为任何(引用,而不是原始)类型。例如:
TableColumn<FileMP3, Integer> yearCol = new TableColumn<>("Year");
yearCol.setCellValueFatory(new PropertyValueFactory<FileMP3, Integer>("year"));
Run Code Online (Sandbox Code Playgroud)
与 Duration(而不是 Integer)类似。
默认情况下,将通过对单元格中的值调用 toString() 来显示单元格中的值。如果希望值以不同方式显示,可以创建自定义单元工厂(不同于单元值工厂):
TableColumn<FileMP3, Integer> durationCol = new TableColumn<>("Duration");
durationCol.setCellValueFactory(new PropertyValueFactory<FileMP3, Duration>("duration"));
durationCol.setCellFactory(new Callback<TableColumn<FileMP3, Duration>, TableCell<FileMP3, Duration>>() {
@Override
public TableCell<FileMP3, Duration> call(TableColumn<FileMP3, Duration> col) {
return new TableCell<FileMP3, Duration>() {
@Override
protected void updateItem(Duration duration, boolean empty) {
super.updateItem(duration, empty);
if (empty) {
setText(null);
} else {
setText(Double.toString(duration.toMillis());
}
}
};
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10489 次 |
| 最近记录: |