[英]JavaFX TableView edit not editing
在JavaFX 8中,我試圖在表格中添加新行后讓單元格進行編輯,以優化用戶體驗。
hourTableView.getSelectionModel().select(newHour);
int row = hourTableView.getSelectionModel().getSelectedIndex();
hourTableView.edit(row, hourTableAmountTableColumn);
選擇正確的行,但是該單元格不會進入編輯模式。 好吧,我已經看到它是偶然發生的,但是很難再現。 我究竟做錯了什么?
如果要在剛剛插入的行上開始編輯,該表的行為會很奇怪。 我可以毫無問題地開始編輯任何行。 但是新添加的行沒有按預期工作。 我用線程延遲了編輯調用,然后它起作用了……請參見以下示例代碼:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class jfxtest extends Application {
private ObservableList<Person> data;
private TableView<Person> tableView;
private TableColumn<Person, String> firstNameCol;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Button addButton = new Button("add new row");
addButton.setOnMouseClicked(event -> addRow());
VBox vbox = new VBox(addButton, createContent());
primaryStage.setScene(new Scene(vbox));
primaryStage.show();
}
private void addRow() {
data.add(new Person("peter", "parker", "spiderman@aol.com"));
new Thread(() -> {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
Platform.runLater(() -> {
int row = data.size() - 1;
tableView.getSelectionModel().select(row);
tableView.getSelectionModel().focus(row);
tableView.edit(row, firstNameCol);
});
}).start();
}
private TableView createContent() {
data = FXCollections.observableArrayList(
new Person("Jacob", "Smith", "jacob.smith@example.com"),
new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
new Person("Ethan", "Williams", "ethan.williams@example.com"),
new Person("Emma", "Jones", "emma.jones@example.com"),
new Person("Michael", "Brown", "michael.brown@example.com")
);
firstNameCol = new TableColumn<>("First");
firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));
firstNameCol.setEditable(true);
firstNameCol.setCellFactory(TextFieldTableCell.forTableColumn());
firstNameCol.setOnEditCommit(
t -> t.getTableView().getItems().get(
t.getTablePosition().getRow()).firstNameProperty().setValue(t.getNewValue())
);
TableColumn<Person, String> lastNameCol = new TableColumn<>("Last");
lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));
TableColumn<Person, String> emailCol = new TableColumn<>("Email");
emailCol.setCellValueFactory(new PropertyValueFactory<>("email"));
tableView = new TableView<>();
tableView.setEditable(true);
tableView.setItems(data);
tableView.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
return tableView;
}
public class Person {
private StringProperty firstName;
private StringProperty lastName;
private StringProperty email;
Person(String fName, String lName, String email) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
this.email = new SimpleStringProperty(email);
}
public StringProperty firstNameProperty() {
return firstName;
}
public StringProperty lastNameProperty() {
return lastName;
}
public StringProperty emailProperty() {
return email;
}
}
}
在JavaFX TableView的“插入”行中找到解決方案, 並且開始編輯無法正常工作
您需要先調用hourTableView.layout()
然后再調用hourTableView.edit()
。 當新行不可見時,它會變得更加復雜,我發現hourTableView.scrollTo()
如果在 hourTableView.layout()
之前 hourTableView.layout()
它會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.