TableView中的Java8 ObjectProperty不刷新

时间:2014-07-08 20:40:33

标签: java properties javafx

我有这个人的TableView:

<TableView fx:id="table" editable="true" focusTraversable="false" prefHeight="-1.0" prefWidth="-1.0" tableMenuButtonVisible="false" VBox.vgrow="ALWAYS">
  <columns>
    <TableColumn editable="false" maxWidth="5000.0" minWidth="10.0" prefWidth="100.0" resizable="false" sortable="false" style="-fx-font-size: 13pt;" text="Address">
      <cellValueFactory>
        <PropertyValueFactory property="id" />
      </cellValueFactory>
    </TableColumn>
    <TableColumn editable="false" maxWidth="5000.0" minWidth="10.0" prefWidth="200.0" resizable="false" sortable="false" style="-fx-font-size: 13pt;" text="Address Type">
      <cellValueFactory>
        <PropertyValueFactory property="range" />
      </cellValueFactory>
    </TableColumn>
    <TableColumn editable="false" prefWidth="200.0" resizable="false" sortable="false" style="-fx-font-size: 13pt;" text="Timestamp">
      <cellValueFactory>
        <PropertyValueFactory property="timestamp" />
      </cellValueFactory>
    </TableColumn>
  </columns>
</TableView>  

和班人:

public class People {  
public static enum Range {ONE, TWO, THREE, FOUR}

public People() {
}

private IntegerProperty id = new SimpleIntegerProperty();
public int getId() {
    return id.get();
}
public IntegerProperty idProperty() {
    return id;
}
public void setId(int id) {
    this.id.set(id);
}

private ObjectProperty<Range> range = new SimpleObjectProperty<>();
public Range getRange() {
    return range.get();
}
public ObjectProperty<Range> rangeProperty() {
    return range;
}
public void setRange(Range range) {
    this.range.set(range);
}

private ObjectProperty<Timestamp> timestamp = new SimpleObjectProperty<>();
public Timestamp getTimestamp() {
    return timestamp.get();
}
public ObjectProperty<Timestamp> timestampProperty() {
    return timestamp;
}
public void setTimestamp(Timestamp timestamp) {
    this.timestamp.set(timestamp);
}
}

我的问题是,当我在人员行中设置新的TimeStamp并且显示TableView时,该列不会更新时间戳。我必须使用StringProperties来自动刷新?

1 个答案:

答案 0 :(得分:0)

好的,我得到了解决方案。

我正在以这种方式更新时间戳:

private Timestamp actualTimestamp = new Timestamp();
private void updateTimeStamp(){
  actualTimestamp.setTime(System.currentTimeMillis());
  people.setTimestamp(actualTimestamp);
}

不清楚的原因:我没有设置新的Timestamp对象,因此失效不会弱化。

正确的方式:

private void updateTimeStamp(){
  actualTimestamp.setTime(new Timestamp(System.currentTimeMillis()));
  people.setTimestamp(actualTimestamp);
}