JavaFX setCellValueFactory方法调用更多次
我已经创建了一个tableView。但是它的setCellValueFactory方法调用两次或多次。它会引起一个bug。这是什么原因呢?下面是我的Person和Controller类 Person.javaJavaFX setCellValueFactory方法调用更多次,java,javafx,callback,Java,Javafx,Callback,我已经创建了一个tableView。但是它的setCellValueFactory方法调用两次或多次。它会引起一个bug。这是什么原因呢?下面是我的Person和Controller类 Person.java public class Person { private final SimpleStringProperty name; private final SimpleStringProperty surname; private final SimpleStringProperty jo
public class Person {
private final SimpleStringProperty name;
private final SimpleStringProperty surname;
private final SimpleStringProperty job;
private final BooleanProperty isSelected;
public Person(SimpleStringProperty name, SimpleStringProperty surname, SimpleStringProperty job,
BooleanProperty isSelected) {
super();
this.name = name;
this.surname = surname;
this.job = job;
this.isSelected = isSelected;
}
public SimpleStringProperty getName() {
return name;
}
public SimpleStringProperty getSurname() {
return surname;
}
public SimpleStringProperty getJob() {
return job;
}
public BooleanProperty getIsSelected() {
return isSelected;
}
}
public class Controller {
@FXML
private final TableView<Person> fxPersonTableView;
@FXML
private final TableColumn<Person, Boolean> fxSelectColumnCheckbox;
@FXML
private final TableColumn<Person, String> fxNameTableColumn;
@FXML
private final TableColumn<Person, String> fxSurnameTableColumn;
@FXML
private final TableColumn<Person, String> fxJobTableColumn;
List<Person> persons;
private void createTableView(){
fxSelectColumnCheckbox.setCellFactory(CheckBoxTableCell.forTableColumn(fxSelectColumnCheckbox));
//fxSelectColumnCheckbox.setCellValueFactory(c -> c.getValue().getIsSelected()); I'm using this, but for print wrote below
fxSelectColumnCheckbox.setCellValueFactory(c -> {
System.out.println("METHOD: fxSelectColumnCheckbox.setCellValueFactory"); // This is printed two or more times.
return c.getValue().getIsSelected());
});
fxNameTableColumn.setCellValueFactory(c -> persons.get(c).getName());
fxSurnameTableColumn.setCellValueFactory(c -> persons.get(c).getSurname());
fxJobTableColumn.setCellValueFactory(c -> persons.get(c).getJob());
fxPersonTableView.getSelectionModel.setSleectionMode(SelectionMode.MULTIPLE);
fxPersonTableView.setItems(persons);
}
}
Controller.java
public class Person {
private final SimpleStringProperty name;
private final SimpleStringProperty surname;
private final SimpleStringProperty job;
private final BooleanProperty isSelected;
public Person(SimpleStringProperty name, SimpleStringProperty surname, SimpleStringProperty job,
BooleanProperty isSelected) {
super();
this.name = name;
this.surname = surname;
this.job = job;
this.isSelected = isSelected;
}
public SimpleStringProperty getName() {
return name;
}
public SimpleStringProperty getSurname() {
return surname;
}
public SimpleStringProperty getJob() {
return job;
}
public BooleanProperty getIsSelected() {
return isSelected;
}
}
public class Controller {
@FXML
private final TableView<Person> fxPersonTableView;
@FXML
private final TableColumn<Person, Boolean> fxSelectColumnCheckbox;
@FXML
private final TableColumn<Person, String> fxNameTableColumn;
@FXML
private final TableColumn<Person, String> fxSurnameTableColumn;
@FXML
private final TableColumn<Person, String> fxJobTableColumn;
List<Person> persons;
private void createTableView(){
fxSelectColumnCheckbox.setCellFactory(CheckBoxTableCell.forTableColumn(fxSelectColumnCheckbox));
//fxSelectColumnCheckbox.setCellValueFactory(c -> c.getValue().getIsSelected()); I'm using this, but for print wrote below
fxSelectColumnCheckbox.setCellValueFactory(c -> {
System.out.println("METHOD: fxSelectColumnCheckbox.setCellValueFactory"); // This is printed two or more times.
return c.getValue().getIsSelected());
});
fxNameTableColumn.setCellValueFactory(c -> persons.get(c).getName());
fxSurnameTableColumn.setCellValueFactory(c -> persons.get(c).getSurname());
fxJobTableColumn.setCellValueFactory(c -> persons.get(c).getJob());
fxPersonTableView.getSelectionModel.setSleectionMode(SelectionMode.MULTIPLE);
fxPersonTableView.setItems(persons);
}
}
公共类控制器{
@FXML
私有最终表视图fxPersonTableView;
@FXML
private final TableColumn fxSelectColumnCheckbox;
@FXML
私有最终表列fxNameTableColumn;
@FXML
私有最终TableColumn fxName TableColumn;
@FXML
私有最终表列fxJobTableColumn;
列出人员名单;
私有void createTableView(){
fxSelectColumnCheckbox.setCellFactory(CheckBoxTableCell.forTableColumn(fxSelectColumnCheckbox));
//fxSelectColumnCheckbox.setCellValueFactory(c->c.getValue().getIsSelected());我正在使用它,但用于下面写的打印
fxSelectColumnCheckbox.setCellValueFactory(c->{
System.out.println(“方法:fxSelectColumnCheckbox.setCellValueFactory”);//这会被打印两次或更多次。
返回c.getValue().getIsSelected());
});
fxNameTableColumn.setCellValueFactory(c->persons.get(c.getName());
fxNameTableColumn.setCellValueFactory(c->persons.get(c.getNames());
fxJobTableColumn.setCellValueFactory(c->persons.get(c.getJob());
fxPersonTableView.getSelectionModel.setSleectionMode(SelectionMode.MULTIPLE);
fxPersonTableView.setItems(个人);
}
}
哪些方法被多次调用?您的setCellFactory(…)
方法将在每次调用createTableView()
时调用一次。setCellValueFactory方法将被多次调用。我调用createTableView()一次,但它调用setCellValueFactory方法两次。它会打印两次“METHOD:…”,这就是问题所在。每次单元格需要知道要显示的值时,都会调用传递给setCellFactory
的回调。因此,在开始时,它将为列中的每个非空可见单元格调用一次(必须多次调用它,并将不同的项作为参数传递,以表示不同的行)。随后,如果单元格被重新用于不同的项目(例如,如果用户滚动),则可能会调用它。这是设计的行为,没有办法阻止它;您需要在理解这将发生的情况下编写代码。我理解,谢谢@詹姆斯·奥迪也有同样的问题。请分享解决方案