JavaFX 2: Get TableCell Row Index

后端 未结 2 754
梦毁少年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.

    0 讨论(0)
  • 2021-02-15 22:18

    Almost There

    Before calling commitEdit, it is necessary to call getTableView().edit(getTableRow().getIndex(), param). This puts the cell into "editing mode". Since there is no startEdit method, there is very little involved in entering edit mode, but it is still required.

    After that, as described here: http://download.oracle.com/javafx/2.0/ui_controls/table-view.htm

    It is necessary to call

    firstCol.setOnEditCommit(new EventHandler<EditEvent<String>>() {
        @Override
        public void handle(EditEvent<String> event) {
            String newValue = event.getNewValue();
            ContactOptions data = (ContactOptions) event.getTableView().getItems().get(event.getTablePosition().getRow());
            data.one.set(newValue)
            if(newValue.equals("No")) {
                data.three.set("No");
                data.four.set("No");
            }
        }
    }
    

    Now all I need to know is how to update the table's display once the data is updated.

    0 讨论(0)
提交回复
热议问题