嗨,我只想在JavaFX中使用可编辑的多行单元格。
@FXML
private TableColumn bookName;
@Override
public void initialize(URL url, ResourceBundle rb) {
tableView.setEditable(true);
// setCellFactory();
bookName.setCellFactory(TextFieldTableCell.forTableColumn()); // If I comment this line and uncomment above line then cell will be multiline but not editable
bookName.setOnEditCommit(
new EventHandler<TableColumn.CellEditEvent<Books, String>>() {
@Override
public void handle(TableColumn.CellEditEvent<Books, String> t) {
if (t.getNewValue().equals("") || t.getNewValue().length() < 3) {
info.setText("Book name must be greater than 3 characters.");
((Books) t.getTableView().getItems().get(
t.getTablePosition().getRow())).setBookName(t.getOldValue());
return;
}
((Books) t.getTableView().getItems().get(
t.getTablePosition().getRow())).setBookName(t.getNewValue());
}
});
}
如果我设置自定义cellFactory,则单元格将是多行但不可编辑的
private void setCellFactory() {
Callback<TableColumn, TableCell> cellFactory = new Callback<TableColumn, TableCell>() {
@Override
public TableCell call(TableColumn param) {
final TableCell cell = new TableCell() {
private Text text;
@Override
public void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
if (!isEmpty()) {
text = new Text(item.toString());
text.setWrappingWidth(140);
setGraphic(text);
}
}
};
return cell;
}
};
bookName.setCellFactory(cellFactory);
}
不可编辑的多行单元格。