【问题标题】:Bind two Spinner controls into a TableView in JavaFX在 JavaFX 中将两个 Spinner 控件绑定到一个 TableView 中
【发布时间】:2015-04-22 19:29:20
【问题描述】:

如何将两个 Spinner 控件绑定到一个 TableView 中?根据下面的截图,我想做一些事情: colA = colB / 2 (and colB = colA x 2...) :

这里是用来暴露问题的sn-p(故意简单):

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

【问题讨论】:

    标签: java javafx spinner tableview javafx-8


    【解决方案1】:

    这是在 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());
    

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

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

    【讨论】:

      【解决方案2】:

      试试:

      valA.bind(valB.divide(2));
      

      【讨论】:

      • 好主意!但它不起作用...第一个错误是Bidirectional binding failed, setting to the previous valueMyBean.valA : A bound value cannot be set.引起的
      • @TibUs 你想要的是一个双向绑定,在两者之间有一个表达式,对吧?如果是这样,我也会对答案感兴趣,但很难弄清楚;-)
      • @kleopatra 是的,这正是我想要的!我还发现this link 这似乎是解决方案的开始......
      • @TibUs 感谢您的链接-评论中引用的博客 wittcarl.deneb.uberspace.de/wordpress/… 中的绑定确实有效(以 Property 与 IntegerProperty 的常见怪异为模)
      猜你喜欢
      • 2014-08-06
      • 2023-03-12
      • 2013-08-21
      • 2018-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-14
      • 1970-01-01
      相关资源
      最近更新 更多