R
I'll start with an expanded example from http://docs.oracle.com/javafx/2/ui_controls/table-view.htm :public class TableViewSample extends Application {
public static final String Column1MapKey = "String";
public static final String Column2MapKey = "Integer";
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Table View");
stage.setWidth(300);
stage.setHeight(500);
final Label label = new Label("TODO List");
label.setFont(new Font("Arial", 20));
TableColumn<Map, String> firstDataColumn = new TableColumn<>("Description");
TableColumn<Map, String> secondDataColumn = new TableColumn<>("Value");
firstDataColumn.setCellValueFactory(new MapValueFactory(Column1MapKey));
firstDataColumn.setMinWidth(130);
secondDataColumn.setCellValueFactory(new MapValueFactory(Column2MapKey));
secondDataColumn.setMinWidth(130);
TableView table_view = new TableView<>(generateDataInMap());
table_view.setEditable(true);
table_view.getSelectionModel().setCellSelectionEnabled(true);
table_view.getColumns().setAll(firstDataColumn, secondDataColumn);
firstDataColumn.setCellFactory(new Callback<TableColumn<Map, String>, TableCell<Map, String>>() {
@Override
public TableCell call(TableColumn p) {
return new TextFieldTableCell(new DefaultStringConverter());
}
});
secondDataColumn.setCellFactory(new Callback<TableColumn<Map, String>, TableCell<Map, String>>() {
@Override
public TableCell call(TableColumn p) {
return new TextFieldTableCell(new IntegerStringConverter());
}
});
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table_view);
((Group) scene.getRoot()).getChildren().addAll(vbox);
stage.setScene(scene);
stage.show();
}
private ObservableList<Map> generateDataInMap() {
int max = 10;
ObservableList<Map> allData = FXCollections.observableArrayList();
for (int i = 1; i < max; i++) {
Map<String, Property<?>> dataRow = new HashMap<>();
SimpleStringProperty value1 = new SimpleStringProperty("string " + i);
SimpleIntegerProperty value2 = new SimpleIntegerProperty(i);
dataRow.put(Column1MapKey, value1);
dataRow.put(Column2MapKey, value2);
allData.add(dataRow);
}
return allData;
}
}
And on the question:Call attention to the CellValueFactory method - fills boxes, while the dynamic change is the result of the setCellFactory method, which, through a long chain, is responsible for interaction with the Property object - the original example shows the work with the abstract Class StringConverter - a good way out of many situations.Yeah, great janerik.UPD:
Like SimpleObjectProperty Bread class:class Wrapper {
class InnerWrapper<T> extends SimpleObjectProperty<T> {
@Override
public void set(T newValue) {
T temp = null;
if (newValue.getClass().equals(Integer.class))
temp = newValue;
else
temp = (T) (((String) newValue).matches("\\d+") ? Integer.valueOf((String) newValue) : newValue.toString());
super.set(temp);
}
}
private InnerWrapper inner = new InnerWrapper();
public Object getNut() {
return nutProperty().get();
}
public final SimpleObjectProperty nutProperty() {
return inner;
}
public void setNut(Object candiate) {
nutProperty().set(candiate);
}
}
Example of factory: firstDataColumn.setCellValueFactory(new Callback<CellDataFeatures<Map, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<Map, String> p) {
return ((Wrapper)p.getValue().get(Column1MapKey)).nutProperty();
}
});