如何指定对象如何转换为字符串以在JavaFX TableView中显示它?

问题描述 投票:0回答:2

我有一个TableView,其中有几列用FXML创建:

<TableView fx:id="logTable" BorderPane.alignment="CENTER">
    <columns>
        <TableColumn fx:id="timestampColumn" editable="false" text="Timestamp">
            <cellValueFactory>
                <PropertyValueFactory property="timestamp"/>
            </cellValueFactory>
        </TableColumn>
        <TableColumn fx:id="actionColumn" editable="false" text="Action">
            <cellValueFactory>
                <PropertyValueFactory property="action"/>
            </cellValueFactory>
        </TableColumn>
    </columns>
</TableView>

然后我有一个像这样定义的对象:

private ObservableList<LogEntry> log = FXCollections.observableArrayList();

它被设置为TableView的模型:

logTable.setItems(log);

LogEntries看起来像这样:

import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import org.joda.time.DateTime;

public class LogEntry {
    private SimpleObjectProperty<DateTime> timestamp = new SimpleObjectProperty<>();
    private SimpleStringProperty action = new SimpleStringProperty();

    public LogEntry(String format, Object... args) {
        this.timestamp.setValue(new DateTime());
        String s = String.format(format, args);
        System.out.println(s);
        this.action.setValue(s);
    }

    public DateTime getTimestamp() {
        return timestamp.getValue();
    }

    public String getAction() {
        return action.getValue();
    }
}

我的问题是,如何指定如何将Jodatime DateTimes转换为字符串进行显示?我想使用当前的语言环境转换它们(但我希望对该列进行排序仍然有效)。

java javafx tableview jodatime
2个回答
1
投票

我没有和Joda Time合作过,但是和LocalDateTime做过类似的事情。

这是一个如何工作的例子。

首先,您需要公开属性::

public class LogEntry {
    private SimpleObjectProperty<LocalDateTime> timestamp = new SimpleObjectProperty<>();
    private SimpleStringProperty action = new SimpleStringProperty();
    public final SimpleObjectProperty<LocalDateTime> timestampProperty() {
        return this.timestamp;
    }

    public final java.time.LocalDateTime getTimestamp() {
        return this.timestampProperty().get();
    }

    public final void setTimestamp(final java.time.LocalDateTime timestamp) {
        this.timestampProperty().set(timestamp);
    }

    public final SimpleStringProperty actionProperty() {
        return this.action;
    }

    public final java.lang.String getAction() {
        return this.actionProperty().get();
    }

    public final void setAction(final java.lang.String action) {
        this.actionProperty().set(action);
    }
}

然后设置单元格工厂和单元格值工厂:

dateTimeColumn.setCellFactory(tc -> new LocalDateTimeTableCell<LogEntry>(true));
dateTimeColumn.setCellValueFactory(data -> data.getValue().timestampProperty());

像这样创建一个表格单元格:

public class LocalDateTimeTableCell<S> extends TableCell<S, LocalDateTime> {
    private final DateTimeFormatter myDateFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
    private final DateTimeFormatter myDateTimeFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm:ss a");
    private final boolean showTime;

    public LocalDateTimeTableCell(boolean showTime){
        this.showTime = showTime;
    }
    @Override
    protected void updateItem(LocalDateTime item, boolean empty) {
        super.updateItem(item, empty);
        if (item == null || empty) {
            setText(null);
            setStyle("");
        } else {
            // Format date.
            if(showTime) {
                setText(myDateTimeFormatter.format(item));
            }else {
                setText(myDateFormatter.format(item));
            }
        }
    }
}

我知道这不是你在Joda时间所要求的 - 但是应该给你方向。


-1
投票

使用cell-factory更新单元格

private TableColumn<Customer, LocalDateTime> Col_date;
    Col_date.setCellFactory((TableColumn<LogEntry, LocalDateTime> param) -> {
        TableCell<LogEntry, LocalDateTime> cell = new TableCell<LogEntry, LocalDateTime>() {
            @Override
            public void updateItem(LocalDateTime item, boolean empty) {
                if (item != null) {
                    setText(getDateTimeFormat(item,"yyyy/MM/dd HH:mm"));

                } else {
                    setText(null);
                }
            }
        };
        return cell;
    });


public static String getDateTimeFormat(LocalDateTime dateTime,String format) throws DateTimeParseException {
    return dateTime.format(DateTimeFormatter.ofPattern(format));
}
© www.soinside.com 2019 - 2024. All rights reserved.