将两个Spinner控件绑定到JavaFX中的TableView中

时间:2015-04-22 19:29:21

标签: java javafx spinner tableview javafx-8

如何将两个Spinner控件绑定到TableView中?根据下面的sreenshot,我想做点什么:colA = colB / 2(和colB = colA x 2 ...... ):

exemple screenshot

这里用于揭露问题的片段(故意简单):

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();
    }
}

2 个答案:

答案 0 :(得分: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包装器相比) - 这是一个我完全不理解的打字问题[更新,见下文](我和泛型永远不会成为朋友;-)这阻碍了成功的双向绑定:

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));
            }
        }
    }
}

更新(了解.asObject的问题)

问题不在于打字,而是(再次打击!)双向绑定中的弱监听器注册:

// 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());

动态创建的包装器是一个本地引用,只要包含方法,就可以(并且是)垃圾收集......就像这些弱的监听上下文一样,没有(至少没有我知道的)其中的替代方案令人满意:

  • 放弃Spinner的输入:使用Number(vs.Integer)不需要包装器,因为InterProperty instanceOf ObjectProperty<Number>
  • 在某处保留对包装器的强引用

答案 1 :(得分:0)

尝试:

valA.bind(valB.divide(2));
相关问题