I\'m trying to a add a Combo Box
to my Table View
:
Basically I have a class called TableViewTest that stores a name and a description, I ca
James_D's answer works well, but requires the user to click on the item to see the ComboBox
. If you want to have ComboBox
es in a column, that are always shown, you have to use a custom cellFactory
:
public class TableViewTest {
...
private final StringProperty option = new SimpleStringProperty();
public String getOption() {
return option.get();
}
public void setOption(String value) {
option.set(value);
}
public StringProperty optionProperty() {
return option;
}
}
TableColumn column = new TableColumn<>("option");
column.setCellValueFactory(i -> {
final StringProperty value = i.getValue().optionProperty();
// binding to constant value
return Bindings.createObjectBinding(() -> value);
});
column.setCellFactory(col -> {
TableCell c = new TableCell<>();
final ComboBox comboBox = new ComboBox<>(options);
c.itemProperty().addListener((observable, oldValue, newValue) -> {
if (oldValue != null) {
comboBox.valueProperty().unbindBidirectional(oldValue);
}
if (newValue != null) {
comboBox.valueProperty().bindBidirectional(newValue);
}
});
c.graphicProperty().bind(Bindings.when(c.emptyProperty()).then((Node) null).otherwise(comboBox));
return c;
});