将两个 Spinner 控件绑定到 JavaFX 中的 TableView
Bind two Spinner controls into a TableView in JavaFX
如何将两个 Spinner 控件绑定到一个 TableView 中?根据下面的截图,我想做一些事情:colA = colB / 2 (and colB = colA x 2...) :
这里是用来暴露问题的片段(故意简单):
TestApp.java
public class TestApp extends Application {
@Override
public void start(Stage stage) throws Exception {
final TableView<MyBean> tableView = new TableView<>();
final TableColumn<MyBean, Integer> colA = new TableColumn<>("Col A");
final TableColumn<MyBean, Integer> colB = new TableColumn<>("Col B");
colA.setCellFactory(col -> new SpinnerCell<MyBean, Integer>());
colA.setCellValueFactory(new PropertyValueFactory<MyBean, Integer>("valA"));
colB.setCellFactory(col -> new SpinnerCell<MyBean, Integer>());
colB.setCellValueFactory(new PropertyValueFactory<MyBean, Integer>("valB"));
tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
tableView.setItems(FXCollections.observableArrayList(new MyBean(1, 2)));
tableView.getColumns().addAll(colA, colB);
stage.setScene(new Scene(new VBox(tableView), 500, 300));
stage.show();
}
public static void main(String[] args) {
Application.launch();
}
}
SpinnerCell.java
public class SpinnerCell<S, T> extends TableCell<S, T> {
private Spinner<Integer> spinner;
private ObservableValue<T> ov;
public SpinnerCell() {
this.spinner = new Spinner<Integer>(0, 100, 1);
setAlignment(Pos.CENTER);
}
@Override
protected void updateItem(Integer item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
setText(null);
setGraphic(this.spinner);
if(this.ov instanceof IntegerProperty) {
this.spinner.getValueFactory().valueProperty().unbindBidirectional(((IntegerProperty) this.ov).asObject());
}
this.ov = getTableColumn().getCellObservableValue(getIndex());
if(this.ov instanceof IntegerProperty) {
this.spinner.getValueFactory().valueProperty().bindBidirectional(((IntegerProperty) this.ov).asObject());
}
}
}
}
MyBean.java
public class MyBean {
private IntegerProperty valA, valB;
public MyBean(int valA, int valB) {
this.valA = new SimpleIntegerProperty(this, "valA", valA);
this.valB = new SimpleIntegerProperty(this, "valB", valB);
}
public IntegerProperty valAProperty() {
return this.valA;
}
public void setValA(int valA) {
this.valA.set(valA);
}
public int getValA() {
return valA.get();
}
public IntegerProperty valBProperty() {
return this.valB;
}
public void setValB(int valB) {
this.valB.set(valB);
}
public int getValB() {
return valB.get();
}
}
尝试:
valA.bind(valB.divide(2));
下面是在 MyBean 中使用 extended bidirectional binding support 的示例:
public static class MyBean {
private IntegerProperty valA;
private IntegerProperty valB;
public MyBean(int valA) {
this.valA = new SimpleIntegerProperty(this, "valA", valA);
this.valB = new SimpleIntegerProperty(this, "valB", 0);
updateB(this.valA, null, this.valA.get());
BidirectionalBinding.<Number, Number>bindBidirectional(
this.valA, this.valB, this::updateB, this::updateA);
}
protected void updateB(ObservableValue<? extends Number> source, Number old, Number value) {
setValB(value.intValue() * 2);
}
protected void updateA(ObservableValue<? extends Number> source, Number old, Number value) {
setValA(value.intValue() / 2);
}
... // same as in OP's code
}
另外,在 SpinnerCell 中,直接绑定到 bean 属性(相对于它的 asObject 包装器)- 有一个我不完全理解的输入问题 [update,见下文](我和泛型永远不会成为朋友 ;-) 这阻碍了成功的双向绑定:
public static class SpinnerCell<S, T extends Number> extends TableCell<S, T> {
private Spinner<T> spinner;
private ObservableValue<T> ov;
public SpinnerCell() {
this(1);
}
public SpinnerCell(int step) {
this.spinner = new Spinner<>(0, 100, step);
setAlignment(Pos.CENTER);
}
@Override
protected void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
setText(null);
setGraphic(this.spinner);
if(this.ov instanceof Property) {
this.spinner.getValueFactory().valueProperty().unbindBidirectional(((Property) this.ov));
}
this.ov = getTableColumn().getCellObservableValue(getIndex());
if(this.ov instanceof Property) {
this.spinner.getValueFactory().valueProperty().bindBidirectional(((Property) this.ov));
}
}
}
}
Update(理解.asObject的问题)
问题不在于输入本身,而是(再次打击!)bidi-binding 中的弱监听器注册:
// spinner type
Spinner<Integer> spinner;
// value type (in valueFactory):
ObjectProperty<Integer> valueProperty;
// value type in bean:
IntegerProperty valXProperty;
// to be bindeable to spinner's value, needs to be wrapped
// into ObjectProperty<Integer>
// intuitively ... WRONG!
valueProperty.bindBidirectional(bean.valXProperty().asObject());
动态创建的包装器是一个本地引用,一旦包含方法被遗留下来,它就可以(并且正在)被垃圾收集......与这些弱监听上下文一样,none(?至少none 我知道)的替代方案令人满意:
- 放松 Spinner 的输入:使用 Number (vs.Integer) 不需要包装器,因为
InterProperty instanceOf ObjectProperty<Number>
- 在某处保持对包装器的强引用
如何将两个 Spinner 控件绑定到一个 TableView 中?根据下面的截图,我想做一些事情:colA = colB / 2 (and colB = colA x 2...) :
这里是用来暴露问题的片段(故意简单):
TestApp.java
public class TestApp extends Application {
@Override
public void start(Stage stage) throws Exception {
final TableView<MyBean> tableView = new TableView<>();
final TableColumn<MyBean, Integer> colA = new TableColumn<>("Col A");
final TableColumn<MyBean, Integer> colB = new TableColumn<>("Col B");
colA.setCellFactory(col -> new SpinnerCell<MyBean, Integer>());
colA.setCellValueFactory(new PropertyValueFactory<MyBean, Integer>("valA"));
colB.setCellFactory(col -> new SpinnerCell<MyBean, Integer>());
colB.setCellValueFactory(new PropertyValueFactory<MyBean, Integer>("valB"));
tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
tableView.setItems(FXCollections.observableArrayList(new MyBean(1, 2)));
tableView.getColumns().addAll(colA, colB);
stage.setScene(new Scene(new VBox(tableView), 500, 300));
stage.show();
}
public static void main(String[] args) {
Application.launch();
}
}
SpinnerCell.java
public class SpinnerCell<S, T> extends TableCell<S, T> {
private Spinner<Integer> spinner;
private ObservableValue<T> ov;
public SpinnerCell() {
this.spinner = new Spinner<Integer>(0, 100, 1);
setAlignment(Pos.CENTER);
}
@Override
protected void updateItem(Integer item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
setText(null);
setGraphic(this.spinner);
if(this.ov instanceof IntegerProperty) {
this.spinner.getValueFactory().valueProperty().unbindBidirectional(((IntegerProperty) this.ov).asObject());
}
this.ov = getTableColumn().getCellObservableValue(getIndex());
if(this.ov instanceof IntegerProperty) {
this.spinner.getValueFactory().valueProperty().bindBidirectional(((IntegerProperty) this.ov).asObject());
}
}
}
}
MyBean.java
public class MyBean {
private IntegerProperty valA, valB;
public MyBean(int valA, int valB) {
this.valA = new SimpleIntegerProperty(this, "valA", valA);
this.valB = new SimpleIntegerProperty(this, "valB", valB);
}
public IntegerProperty valAProperty() {
return this.valA;
}
public void setValA(int valA) {
this.valA.set(valA);
}
public int getValA() {
return valA.get();
}
public IntegerProperty valBProperty() {
return this.valB;
}
public void setValB(int valB) {
this.valB.set(valB);
}
public int getValB() {
return valB.get();
}
}
尝试:
valA.bind(valB.divide(2));
下面是在 MyBean 中使用 extended bidirectional binding support 的示例:
public static class MyBean {
private IntegerProperty valA;
private IntegerProperty valB;
public MyBean(int valA) {
this.valA = new SimpleIntegerProperty(this, "valA", valA);
this.valB = new SimpleIntegerProperty(this, "valB", 0);
updateB(this.valA, null, this.valA.get());
BidirectionalBinding.<Number, Number>bindBidirectional(
this.valA, this.valB, this::updateB, this::updateA);
}
protected void updateB(ObservableValue<? extends Number> source, Number old, Number value) {
setValB(value.intValue() * 2);
}
protected void updateA(ObservableValue<? extends Number> source, Number old, Number value) {
setValA(value.intValue() / 2);
}
... // same as in OP's code
}
另外,在 SpinnerCell 中,直接绑定到 bean 属性(相对于它的 asObject 包装器)- 有一个我不完全理解的输入问题 [update,见下文](我和泛型永远不会成为朋友 ;-) 这阻碍了成功的双向绑定:
public static class SpinnerCell<S, T extends Number> extends TableCell<S, T> {
private Spinner<T> spinner;
private ObservableValue<T> ov;
public SpinnerCell() {
this(1);
}
public SpinnerCell(int step) {
this.spinner = new Spinner<>(0, 100, step);
setAlignment(Pos.CENTER);
}
@Override
protected void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
setText(null);
setGraphic(this.spinner);
if(this.ov instanceof Property) {
this.spinner.getValueFactory().valueProperty().unbindBidirectional(((Property) this.ov));
}
this.ov = getTableColumn().getCellObservableValue(getIndex());
if(this.ov instanceof Property) {
this.spinner.getValueFactory().valueProperty().bindBidirectional(((Property) this.ov));
}
}
}
}
Update(理解.asObject的问题)
问题不在于输入本身,而是(再次打击!)bidi-binding 中的弱监听器注册:
// spinner type
Spinner<Integer> spinner;
// value type (in valueFactory):
ObjectProperty<Integer> valueProperty;
// value type in bean:
IntegerProperty valXProperty;
// to be bindeable to spinner's value, needs to be wrapped
// into ObjectProperty<Integer>
// intuitively ... WRONG!
valueProperty.bindBidirectional(bean.valXProperty().asObject());
动态创建的包装器是一个本地引用,一旦包含方法被遗留下来,它就可以(并且正在)被垃圾收集......与这些弱监听上下文一样,none(?至少none 我知道)的替代方案令人满意:
- 放松 Spinner 的输入:使用 Number (vs.Integer) 不需要包装器,因为
InterProperty instanceOf ObjectProperty<Number>
- 在某处保持对包装器的强引用