java - 在 JavaFX 中将两个 Spinner 控件绑定(bind)到 TableView 中

标签 java javafx spinner tableview javafx-8

如何将两个 Spinner 控件绑定(bind)到 TableView 中?根据下面的截图,我想做一些事情:colA = colB/2(and 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();
    }
}

最佳答案

这是使用 extended bidirectional binding support 的示例在 MyBean 中:

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 中,直接绑定(bind)到 bean 属性(相对于其 asObject 包装器) - 有一个我不完全理解的打字问题 [更新,见下文](我和泛型永远不会成为 friend ;-)这阻碍了成功的双向绑定(bind):

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 的问题)

问题不在于输入本身,而是(再次受到打击!)双向绑定(bind)中的弱监听器注册:

// 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:使用数字(相对于整数)不需要包装器,因为 InterProperty instanceOf ObjectProperty<Number>
  • 在某处保留对包装器的强引用

关于java - 在 JavaFX 中将两个 Spinner 控件绑定(bind)到 TableView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29807105/

相关文章:

Java Gui,隐藏其中一些元素后元素之间的间隙

java - 读取大文件进行模拟(Java 因堆空间不足而崩溃)

JavaFX8 treeTableView自定义折叠根项

java - 应用比例时不支持路径转换

android - 您能否根据 Activity 隐藏布局中的元素,例如微调器?

java - Java 列表中的数字

java - 如何获取项目的 mimetype?

java - 更新到 gradle 3.3 后构建失败

android - 如何在 android 中创建多个微调器并将该微调器数据传递给另一个 Activity (相同)

javascript - 调用后不会立即出现“spinner”