假设我有一个表示数据库表的对象,它有属性,表示当前选中的行:
class MyTable {
private IntegerProperty currentRow ...
public IntegerProperty currentRowProperty() {
return currentRow;
}
public int getCurrentRow() {
return currentRow.get();
}
public void setCurrentRow(int newValue) {
currentRow.setValue(newValue);
}
}
现在,我希望有额外的显式只读实体来表示该行是否可以移动到上一行(绑定到“上一步”按钮)。
如果我使用绑定来实现它
class MyTable {
private BooleanBinding previousExist = currentRowProperty().greaterThan(0);
public BooleanBinding previousExistBinding() {
return previousExist;
}
public boolean isPreviousExist() {
return previousExist.get();
}
}
我将违反JavaFX属性模式,因为返回的类将是绑定,而不是属性。
因此,我需要将结果包装到属性中,但是如何包装呢?
如果我写
class MyTable {
private ReadOnlyBooleanPropertyBase previousExist = new ReadOnlyBooleanPropertyBase() {
@Override
public boolean get() {
return getIndex() >= 0;
}
...
}
}
我将无法依赖更改报告,并且将被要求显式地监听索引更改并将它们发送出去。
那么,如何实现呢?
发布于 2016-05-06 09:15:03
private ReadOnlyBooleanWrapper previousExist;
{
ReadOnlyBooleanWrapper ans = new ReadOnlyBooleanWrapper();
ans.bind( currentRowProperty().greaterThan(0) );
previousExist = ans;
}
public ReadOnlyBooleanProperty previousExist() {
return previousExist.getReadOnlyProperty();
}
public boolean isPreviousExist() {
return previousExist().get();
}
https://stackoverflow.com/questions/37068378
复制