JavaFX:将TableView列与另一个TableView的行数绑定

时间:2014-06-16 10:57:31

标签: tableview javafx-8

我有TableView RoadsName列和Number of Lanes列。 Number of Lanes显示一个整数,即道路的车道数。添加新道路时,我有另一个TableView,其中设置了道路的属性。我的Lane课程是:

public class Lane {

    private StringProperty name;

    private FloatProperty width;

    private BooleanProperty normalDirection;

    public Lane(String name, float width, boolean normalDirection) {
        this.name = new SimpleStringProperty(name);
        this.width = new SimpleFloatProperty(width);
        this.normalDirection = new SimpleBooleanProperty(normalDirection);
    }

    public void setName(String value) { 
        nameProperty().set(value); 
    }

    public String getName() { 
        return nameProperty().get();
    }

    public StringProperty nameProperty() { 
        if (name == null) {
            name = new SimpleStringProperty(this, "name");
        }
        return name; 
    }

    public void setWidth(float value) { 
        widthProperty().set(value); 
    }

    public float getWidth() { 
        return widthProperty().get();
    }

    public FloatProperty widthProperty() { 
        if (width == null) {
            width = new SimpleFloatProperty(this, "width");
        }
        return width; 
    }

    public void setNormalDirection(boolean value) { 
        normalDirectionProperty().set(value);
    }

    public boolean getNormalDirection() { 
        return normalDirectionProperty().get();
    }

    public BooleanProperty normalDirectionProperty() { 
        if (normalDirection == null) normalDirection = new SimpleBooleanProperty(this, "normalDirection");
        return normalDirection; 
    } 
}

我正在尝试创建一个类Road,我希望将属性private IntegerProperty numberOfLanes绑定到ObservableList<Lane>通道中使用的TableView的大小,但我不知道最好的方法是什么。

我是JavaFX世界的新手,感谢任何帮助。谢谢你提前。

1 个答案:

答案 0 :(得分:0)

您正在寻找类似的东西:

public class Road {
   private final ObservableList<Lane> lanes = FXCollections.observableArrayList();
   public final ObservableList<Lane> getLanes() {
      return lanes ;
   }

   private final ReadOnlyIntegerWrapper numberOfLanes = new ReadOnlyIntegerWrapper(this, "numberOfLanes");
   public final int getNumberOfLanes() {
      return numberOfLanes.get();
   }
   public ReadOnlyIntegerProperty numberOfLanesProperty() {
      return numberOfLanes.getReadOnlyProperty();
   }

   public Road() {
      numberOfLanes.bind(Bindings.size(lanes));
   }
}