Reputation: 3225
I have this code:
public static void main(String[] ar){ launch(ar); }
@Override
public void start(Stage primaryStage){
TableView<Foo> tableView = new TableView<>();
tableView.setColumnResizePolicy(TableView.UNCONSTRAINED_RESIZE_POLICY);
tableView.setPlaceholder(new Label("No data"));
tableView.setEditable(true);
TableColumn<Foo, String> colText = new TableColumn<>("Text");
colText.setCellValueFactory(new PropertyValueFactory<>("text"));
tableView.getColumns().add(colText);
List<Foo> listFoo = new ArrayList<>(Arrays.asList(new Foo("1"), new Foo("2"), new Foo("3")));
tableView.setItems(FXCollections.observableArrayList(listFoo));
primaryStage.setScene(new Scene(tableView));
primaryStage.show();
}
public class Foo{
private String text;
public Foo(String txt){ text = txt; }
public String getText(){ return text; }
public void setText(String text){ this.text = text; }
}
I try to make the cells of table editable: tableView.setEditable(true);
, but it don't work.
How can I make cells of table editable or to have posibility to copy the value from cell into clipboard.
Upvotes: 2
Views: 226
Reputation: 82461
For a TableCell
to be editable, 3 conditions need to be fulfilled:
TableView
is editableTableColumn
is editableIn your case the 3rd condition is not fulfilled, since the cells created by the default cellFactory
are not editable. You need to assign a cellFactory
that allows for editing. Furthermore note that unless the cellValueFactory
returns an object implementing WritableValue
, you also need to use the onEditCommit
handler to store the data in the item:
colText.setCellFactory(TextFieldTableCell.forTableColumn());
colText.setOnEditCommit(evt -> evt.getRowValue().setText(evt.getNewValue()));
Upvotes: 9