将整数(非字符串)数据插入JavaFX2表视图
因此,我有一个工作正常的表,并使用下面的代码从将整数(非字符串)数据插入JavaFX2表视图,java,javafx,javafx-2,tableview,Java,Javafx,Javafx 2,Tableview,因此,我有一个工作正常的表,并使用下面的代码从可观察列表中获取数据: public void setMainTableData(ObservableList<FileMP3> list) { artistCol.setCellValueFactory(new PropertyValueFactory<FileMP3, String>("artist")); albumCol.setCellValueFactory(new Prope
可观察列表
中获取数据:
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);
}
public void setMainTableData(可观察列表)
{
artistCol.setCellValueFactory(新财产价值工厂(“艺术家”);
albumCol.setCellValueFactory(新属性值工厂(“album”);
titleCol.setCellValueFactory(新财产价值工厂(“标题”);
trackCol.setCellValueFactory(新属性ValueFactory(“track”));
yearCol.setCellValueFactory(新财产价值工厂(“年”);
mainTable.setItems(列表);
}
但是,这些列并不都包含字符串数据-我需要能够插入int
,以及可能的其他类型,如Duration
。轨迹
和年份
条目存储为整数,并且有一个名为长度
的条目(未显示)。这是作为持续时间
存储在我的FileMP3
对象中的,在将数据插入表之前,我看不到任何明显的方法来操作存储在那里的数据。我希望能够使用Duration.getMillis()
,然后对其执行一些数学运算,将其转换为可显示的int
格式,但我希望将其存储在文件mp3
中作为Duration
我在本主题上阅读的所有教程都使用构造函数:
newpropertyvaluefactory(“流派”)
总之,我希望能够在表中插入除
字符串以外的内容。您可以提供自定义单元格值工厂:
duration.setCellValueFactory(new Callback<CellDataFeatures<FileMP3, Integer>, ObservableValue<Integer>>() {
@Override public ObservableValue<Integer> call(CellDataFeatures<FileMP3, Integer> c) {
return new SimpleIntegerProperty(c.getValue().getDurationAsInt()));
}
});
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());
}
}
};
}
});
duration.setCellValueFactory(新回调(){
@覆盖公共observeValue调用(CellDataFeatures c){
返回新的SimpleIntegerProperty(c.getValue().getDurationAsInt());
}
});
您可以提供自定义单元格值工厂:
duration.setCellValueFactory(new Callback<CellDataFeatures<FileMP3, Integer>, ObservableValue<Integer>>() {
@Override public ObservableValue<Integer> call(CellDataFeatures<FileMP3, Integer> c) {
return new SimpleIntegerProperty(c.getValue().getDurationAsInt()));
}
});
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());
}
}
};
}
});
duration.setCellValueFactory(新回调(){
@覆盖公共observeValue调用(CellDataFeatures c){
返回新的SimpleIntegerProperty(c.getValue().getDurationAsInt());
}
});
您只需将字符串替换为任何(引用,而非原语)类型即可。例如:
TableColumn<FileMP3, Integer> yearCol = new TableColumn<>("Year");
yearCol.setCellValueFatory(new PropertyValueFactory<FileMP3, Integer>("year"));
TableColumn yearCol=新的TableColumn(“年”);
yearCol.setCellValueFatory(新财产价值工厂(“年”);
与持续时间类似(而不是整数)
默认情况下,通过对单元格中的值调用toString()来显示单元格中的值。如果希望以不同方式显示值,可以创建自定义单元工厂(与单元值工厂不同):
TableColumn持续时间col=新的TableColumn(“持续时间”);
durationCol.setCellValueFactory(新属性值工厂(“duration”);
durationCol.setCellFactory(新回调(){
@凌驾
公共TableCell调用(TableColumn列){
返回新的TableCell(){
@凌驾
受保护的void updateItem(持续时间,布尔值为空){
super.updateItem(持续时间,空);
if(空){
setText(空);
}否则{
setText(Double.toString(duration.toMillis());
}
}
};
}
});
您可以将字符串替换为任何(引用,而不是基元)类型。例如:
TableColumn<FileMP3, Integer> yearCol = new TableColumn<>("Year");
yearCol.setCellValueFatory(new PropertyValueFactory<FileMP3, Integer>("year"));
TableColumn yearCol=新的TableColumn(“年”);
yearCol.setCellValueFatory(新财产价值工厂(“年”);
与持续时间类似(而不是整数)
默认情况下,单元格中的值将通过对单元格中的值调用toString()来显示。如果希望以不同方式显示值,可以创建自定义单元格工厂(与单元格值工厂不同):
TableColumn持续时间col=新的TableColumn(“持续时间”);
durationCol.setCellValueFactory(新属性值工厂(“duration”);
durationCol.setCellFactory(新回调(){
@凌驾
公共TableCell调用(TableColumn列){
返回新的TableCell(){
@凌驾
受保护的void updateItem(持续时间,布尔值为空){
super.updateItem(持续时间,空);
if(空){
setText(空);
}否则{
setText(Double.toString(duration.toMillis());
}
}
};
}
});