JavaFX 2: Get TableCell Row Index

后端 未结 2 753
梦毁少年i
梦毁少年i 2021-02-15 21:20

I have a Table with checkboxes. I want to change the selection of the checkbox in the first column when I click on the checkbox in the third or fourth column. I want to be able

2条回答
  •  不知归路
    2021-02-15 22:03

    An advantage of using Observables is that the JavaFX UI elements can perform the bindings for you "behind the scenes." In other words, if you implement your data model class as a JavaFX Bean, your UI will update itself automatically whenever it changes. It does this because bindings for the observable data in your model are automatically assigned and change notification events automatically generated.

    But you have to define your data model according to the JavaFX bean paradigm in order for this to happen, otherwise your UI won't update as changes occur.

    Your data model is defined like this:

    public static class ContactOptions {
    
        private final StringProperty one;
        private final StringProperty two;
        private final StringProperty three;
        private final StringProperty four;
    
        ContactOptions(String col1, String col2, String col3, String col4) {
            this.one = new StringProperty(col1);
            this.two = new StringProperty(col2);
            this.three = new StringProperty(col3);
            this.four = new StringProperty(col4);
        }
    
        public String getOne() {
            return one.get();
        }
    
        public String getTwo() {
            return two.get();
        }
    
        public String getThree() {
            return three.get();
        }
    
        public String getFour() {
            return four.get();
        }
    }
    

    For this reply, I will focus only on your 1st instance field, one. To transform this so that it is compliant with the JavaFX bean paradigm for a JavaFX Property, write your code this way, for example:

    public static class ContactOptions {
    
        private final StringProperty one = new SimpleStringProperty();
    
        public final String getOne() { return this.one.get(); }
        public final void setOne(String v) { this.one.set(v); }
        public final StringProperty oneProperty() { return this.one; }
    

    It is possible to write property definitions for a JavaFX bean that provide for a lazier initialization, but this will work. The difference between a Java bean and a JavaFX bean is that you must also provide an accessor for the property (the last line above).

    If you make all your fields into properties similar to the above, you will find that your UI updates to reflect changes.

提交回复
热议问题