(1) In general, will this work, or am I missing something?
Yes, it looks like it should work.
(2) If so, how should I handle the update method?
You probably don't need to.
ObservableList update events and extractors:
-, , ( Javadocs) ObservableList ( ObservableList ). , , . ObservableList - "". , , Slot a textProperty():
public class Slot {
private final StringProperty text = new SimpleStringProperty(this, "text", "");
public StringProperty textProperty() {
return text ;
}
}
ObservableList<Slot>, , , FXCollections.observableArrayList(...) Callback
ObservableList<Slot> slots = FXCollections.observableArrayList(
(Slot slot) -> new Observable[] {slot.textProperty()} );
Callback , Observable s: Observable , - .
GridPane Slot :
, , , Slot , , Slot. , : GridPane .
, Slot textProperty(), , getSlotContents() Label, . :
public class Slot {
private final StringProperty text = new SimpleStringProperty(this, "text", "");
public StringProperty textProperty() {
return text ;
}
public final String getText() {
return textProperty().get();
}
public final void setText(String text) {
textProperty().set(text);
}
private final Label label = new Label();
public Slot(String text) {
label.textProperty().bind(text);
setText(text);
}
public Node getSlotContents() {
return label ;
}
}
GridPane textProperty() Slot: Label, GridPane, .
, ListChangeListener change, wasUpdated() true; wasAdded() wasRemoved(), .
EasyBind, , Slot , :
public class Slot {
private final IntegerProperty column = new SimpleIntegerProperty(this, "column");
public IntegerProperty columnProperty() {
return column ;
}
public final int getColumn() {
return columnProperty().get();
}
public final void setColumn(int column) {
columnProperty().set(column);
}
public Slot(int column, int row) {
column.addListener((obs, oldColumn, newColumn) ->
GridPane.setColumnIndex(getSlotContents(), newColumn.intValue()));
setColumn(column);
setRow(row);
}
}
ListChangeListener , GridPane getSlotContents() Slot s. ListChangeListener:
slots.addListener(new ListChangeListener<Slot>() {
@Override
public void onChanged(ListChangeListener.Change<? extends Slot> change) {
while (change.next()) {
if (change.wasAdded()) {
for (Slot slot : change.getAddedSublist) {
pane.getChildren().add(slot.getSlotContents());
}
} else if (change.wasRemoved()) {
for (Slot slot : change.getRemoved()) {
pane.getChildren().remove(slot.getSlotContents());
}
}
}
}
});
addTo removeFrom Slot.
, Bindings bindContent, ObservableList . EasyBind ObservableList, ObservableList .
,
ObservableList<Node> nodes = EasyBind.map(slots, Slot::getSlotContents);
ObservableList<Node>, getSlotContents() slots.
ListChangeListener :
Bindings.bindContent(pane.getChildren(),
EasyBind.map(slots, Slot::getSlotContents));