問題如何將新的數據正確地添加到TableView中,滾動到它
我想動態數據添加到TableView中並滾動到新的數據的位置。但是,當TableView中一列到達視口大小,我得到這個在控制檯:
2015年4月18日上午九點14分34秒com.sun.javafx.scene.control.skin.VirtualFlow addTrailingCells INFO:索引超過maxCellCount。檢查大小 類javafx.scene.control.TableRow的計算
所以我想我做錯了什麼。我所做的只是使用Oracle的TableView例子,補充一點:
// button to add new data
Button bt = new Button("Add");
bt.setOnAction(e -> {
// insert new item
int i = data.size() + 1;
Person person = new Person("Name " + i, "Name " + i, "Mail " + i);
data.add(person);
// scroll to new item
table.scrollTo(person);
});
同樣的情況,當你使用
table.getItems().add(person);
不是修改名單。
罪魁禍首是scrollTo方法。當您使用scrollTo方法使用行索引時,會發生相同的信息/錯誤。
問題
你如何正確地添加新的項目到TableView中,使信息/錯誤顯示不出來?
代碼
這裏有一個完整的例子。表格已預先填好。如果點擊添加按鈕,則視口尺寸已達到,並且控制檯中顯示信息(或錯誤?)。
public class TableViewSample extends Application {
private TableView<Person> table = new TableView<Person>();
private final ObservableList<Person> data = FXCollections.observableArrayList();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Table View Sample");
stage.setWidth(450);
stage.setHeight(500);
table.setEditable(true);
TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("firstName"));
TableColumn lastNameCol = new TableColumn("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("lastName"));
TableColumn emailCol = new TableColumn("Email");
emailCol.setMinWidth(200);
emailCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("email"));
table.setItems(data);
table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
// -------------------------------
// dynamically add data
// -------------------------------
// fill table with data
for(int i=0; i < 14; i++) {
Person person = new Person("Name " + i, "Name " + i, "Mail " + i);
data.add(person);
}
// button to add new data
Button bt = new Button("Add");
bt.setOnAction(e -> {
// insert new item
int i = data.size() + 1;
Person person = new Person("Name " + i, "Name " + i, "Mail " + i);
data.add(person);
// scroll to new item
table.scrollTo(person);
});
// -------------------------------
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.getChildren().addAll(table, bt);
((Group) scene.getRoot()).getChildren().addAll(vbox);
stage.setScene(scene);
stage.show();
}
public static class Person {
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private final SimpleStringProperty email;
private Person(String fName, String lName, String email) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
this.email = new SimpleStringProperty(email);
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String fName) {
firstName.set(fName);
}
public String getLastName() {
return lastName.get();
}
public void setLastName(String fName) {
lastName.set(fName);
}
public String getEmail() {
return email.get();
}
public void setEmail(String fName) {
email.set(fName);
}
}
}
你要始終滾動到'TableView'的底部? – ItachiUchiha
不,可能存在新行不在底部的用例。示例:焦點位於第5行,用戶向下滾動到第100行。第5行不可見。用戶按下「插入」按鈕。行應添加到位置6,並且應將表格滾動到該新行。 – Roland
對我來說就像是一個bug,因爲只有在'TableView'不包含ScrollBar時纔會發生這種情況。 – eckig