DD.*_*DD. 8 format render cell javafx-2
TableColumn<Event,Date> releaseTime = new TableColumn<>("Release Time");
releaseTime.setCellValueFactory(
new PropertyValueFactory<Event,Date>("releaseTime")
);
Run Code Online (Sandbox Code Playgroud)
如何更改releaseTime的格式?目前它在Date对象上调用一个简单的toString.
小智 9
如果要保留TableColumn的排序功能,则上述解决方案都不是有效的:如果将Date转换为String并在TableView中以这种方式显示; 该表将对其进行排序(如此错误).
我发现的解决方案是继承Date类以覆盖toString()方法.这里有一个警告:TableView使用java.sql.Date而不是java.util.Date; 所以你需要继承前者.
import java.text.SimpleDateFormat;
public class CustomDate extends java.sql.Date {
public CustomDate(long date) {
super(date);
}
@Override
public String toString() {
return new SimpleDateFormat("dd/MM/yyyy").format(this);
}
}
Run Code Online (Sandbox Code Playgroud)
该表将调用该方法以打印日期.
当然,您还需要将TableColumn声明中的Date类更改为新的子类:
@FXML
TableColumn<MyObject, CustomDate> myDateColumn;
Run Code Online (Sandbox Code Playgroud)
将对象属性附加到表的列时,同样如下:
myDateColumn.setCellValueFactory(new PropertyValueFactory< MyObject, CustomDate>("myDateAttr"));
Run Code Online (Sandbox Code Playgroud)
最后,为了清晰起见,这就是你在对象类中声明getter的方法:
public CustomDate getMyDateAttr() {
return new CustomDate(myDateAttr.getTime()); //myDateAttr is a java.util.Date
}
Run Code Online (Sandbox Code Playgroud)
由于它在幕后使用了java.sql.Date,我花了一段时间才弄明白这一点; 所以希望这会为其他人节省一些时间!
我建议使用Java泛型来创建可重用的列格式化程序java.text.Format.这减少了样板代码的数量......
private class ColumnFormatter<S, T> implements Callback<TableColumn<S, T>, TableCell<S, T>> {
private Format format;
public ColumnFormatter(Format format) {
super();
this.format = format;
}
@Override
public TableCell<S, T> call(TableColumn<S, T> arg0) {
return new TableCell<S, T>() {
@Override
protected void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
} else {
setGraphic(new Label(format.format(item)));
}
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
用法示例
birthday.setCellFactory(new ColumnFormatter<Person, Date>(new SimpleDateFormat("dd MMM YYYY")));
amplitude.setCellFactory(new ColumnFormatter<Levels, Double>(new DecimalFormat("0.0dB")));
Run Code Online (Sandbox Code Playgroud)
我最近需要这样做 -
dateAddedColumn.setCellValueFactory(
new Callback<TableColumn.CellDataFeatures<Film, String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<Film, String> film) {
SimpleStringProperty property = new SimpleStringProperty();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
property.setValue(dateFormat.format(film.getValue().getCreatedDate()));
return property;
}
});
Run Code Online (Sandbox Code Playgroud)
但是-这是一个很多使用兰巴表达式在Java中8简单:
dateAddedColumn.setCellValueFactory(
film -> {
SimpleStringProperty property = new SimpleStringProperty();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
property.setValue(dateFormat.format(film.getValue().getCreatedDate()));
return property;
});
Run Code Online (Sandbox Code Playgroud)
赶紧用Java 8发布oracle吧!
Java FX8更新:
(我不确定这是答案的好地方,但是我在JavaFX8中遇到问题,并且某些事情已经改变,例如java.time包)
与以前的答案有些不同:我在列上保留了日期类型,因此我需要同时使用cellValueFactory和cellFactory。我做了一个通用的可重用方法来为所有日期列生成cellFactory。我将java 8 date用于java.time包!但是可以很容易地为java.util.date重新实现该方法。
@FXML
private TableColumn<MyBeanUi, ZonedDateTime> dateColumn;
@FXML
public void initialize () {
// The normal binding to column
dateColumn.setCellValueFactory(cellData -> cellData.getValue().getCreationDate());
//.. All the table initialisation and then
DateTimeFormatter format = DateTimeFormatter .ofLocalizedDate(FormatStyle.SHORT);
dateColumn.setCellFactory (getDateCell(format));
}
public static <ROW,T extends Temporal> Callback<TableColumn<ROW, T>, TableCell<ROW, T>> getDateCell (DateTimeFormatter format) {
return column -> {
return new TableCell<ROW, T> () {
@Override
protected void updateItem (T item, boolean empty) {
super.updateItem (item, empty);
if (item == null || empty) {
setText (null);
}
else {
setText (format.format (item));
}
}
};
};
}
Run Code Online (Sandbox Code Playgroud)
优点是:
你可以通过细胞工厂来实现。参见
/sf/answers/710433531/
/sf/answers/749044971/
虽然第二个链接是 about ListCell,但同样的逻辑也完全适用于TableCells 。
PS 如果您需要一些示例代码,请在此处附上。
| 归档时间: |
|
| 查看次数: |
26285 次 |
| 最近记录: |