我创建了一个TreeView,并将项目设置为ObservableList<ColumnLine>
。我向EditingCell添加了一个监听器,当我按下TAB
时,我可以编辑下一列或下一行的第一列(并将新项添加到项目列表中)。
我不知道为什么它会返回一个大于项目的索引。大小有时。实际上有一个列的单元格值是一个按钮,当我按TAB
(编辑下一列)时它将显示在较大的行上,然后再次按TAB
它将显示回正确的行。
当我按下TAB
时,它会返回一个大于项目的行索引。大小
调用startEdit()时打印内容:
public void startEdit() {
super.startEdit();
if (textField == null) createTextField();
textField.setText(getString());
setGraphic(textField);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
Platform.runLater(
() -> {
textField.requestFocus();
textField.selectAll();
System.out.println(
"start: "
//+ getTableColumn().getText()
+ " row-"
+ getIndex()
+ " column-"
+ getTableView().getColumns().indexOf(getTableColumn())
+ " item count-"
+ getTableView().getItems().size());
System.out.println("item-size: " + getTableView().getItems().size());
});
}
输出:
-- First Line: normal
start: row-0 column-1 item count-1
item-size: 1
commit: row-0 column-1 item count-1
start: row-0 column-2 item count-1
item-size: 1
commit: row-0 column-2 item count-1
size: 2
-- Second Line: wrong
start: row-23 column-1 item count-2 <------ This
item-size: 2
start: row-1 column-1 item count-2
item-size: 2
commit: row-1 column-1 item count-2
start: row-1 column-2 item count-2
item-size: 2
commit: row-1 column-2 item count-2
database next: row-1 col-名
size: 3
-- Third Line: wrong
start: row-22 column-1 item count-3 <------ This
item-size: 3
start: row-2 column-1 item count-3
item-size: 3
commit: row-2 column-1 item count-3
start: row-2 column-2 item count-3
item-size: 3
commit: row-2 column-2 item count-3
database next: row-2 col-名
size: 4
-- Fourth Line: wrong
start: row-3 column-1 item count-4
item-size: 4
commit: row-21 column-1 item count-4 <------ This
start: row-3 column-1 item count-4
item-size: 4
commit: row-3 column-1 item count-4
start: row-3 column-2 item count-4
item-size: 4
commit: row-3 column-2 item count-4
创建一个TableView:
view.getColumns().add(new ColumnBuilder<ColumnLine>().createStringColumn("名", "name"));
view.getColumns().add(new ColumnBuilder<ColumnLine>().createStringColumn("类型", "type"));
view.getColumns().add(new ColumnBuilder<ColumnLine>().createStringColumn("长度", "length"));
view.getColumns().add(new ColumnBuilder<ColumnLine>().createStringColumn("小数点", "decimal"));
TableColumn<ColumnLine, Boolean> colNotNull = new TableColumn<>("不是 null");
colNotNull.setCellFactory(param -> new CheckBoxTableCell<>());
colNotNull.setCellValueFactory(
param -> {
ColumnLine value = param.getValue();
BooleanProperty property = new SimpleBooleanProperty(value.isNotNull());
property.addListener((observable, oldValue, newValue) -> value.setNotNull(newValue));
return property;
});
view.getColumns().add(colNotNull);
TableColumn<ColumnLine, Boolean> key = new TableColumn<>("键");
key.getStyleClass().add("imageValueColumn");
key.setCellFactory(
param ->
new TableCell<ColumnLine, Boolean>() {
private final Button btn = new Button();
{
btn.getStyleClass().add("imageButton");
btn.setOnAction(
event -> {
ColumnLine value = getTableView().getItems().get(getIndex());
value.setPK(!value.isPK());
if (value.isPK()) {
if (btn.getGraphic() == null) {
btn.setGraphic(new ImageView(new Image("/resource/key.png")));
}
} else btn.setGraphic(null);
});
}
@Override
protected void updateItem(Boolean value, boolean empty) {
super.updateItem(value, empty);
setGraphic(empty ? null : btn);
}
});
key.setCellValueFactory(
param -> {
ColumnLine value = param.getValue();
BooleanProperty property = new SimpleBooleanProperty(value.isPK());
property.addListener((observable, oldValue, newValue) -> value.setPK(newValue));
return property;
});
view.getColumns().add(key);
}
编辑单元格的键侦听器(getNextColumn(boolean)将返回下一个单元格):
if (event.getCode() == KeyCode.TAB) {
// Get next cell
TableColumn nextColumn = getNextColumn(true);
if (nextColumn != null) {
// Not the last column
if (nextColumn != getTableView().getColumns().get(1)) {
getTableView().edit(getTableRow().getIndex(), nextColumn);
} else { // the last column
// Not the last line
if (getTableRow().getIndex() < getTableView().getItems().size() - 1) {
// Edit next column
getTableView().edit(getTableRow().getIndex() + 1, nextColumn);
} else { // The last line
// Add a new item
((TableCreator) obj).getNewColumns().add(new ColumnLine());
// Edit
getTableView().edit(getTableRow().getIndex() + 1, nextColumn);
}
}
}
}
}
我不是说英语的人...如果这篇文章中有什么不对,我向你道歉。