JavaFX 2: Get TableCell Row Index

我怕爱的太早我们不能终老 提交于 2019-12-03 13:23:32
Dorothy

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!