我在JavaFX中遇到了TableView的问题。我的表视图的第一个状态是:
然后当我在“Nombre deplacesàréserver”列中编辑我的TableView值时,新值保持显示在我的TableView中,但是当我尝试获取Items并在控制台中显示它们时,我收到了TableView的旧值(它在控制台的第2行显示“0”而不是“8”):
这是窗口的控制器代码:
public class ReserverWindowController {
private SpectacleDto spectacleToReserve;
public TableView<SpectacleReservationDto> listeTarifs = new TableView<SpectacleReservationDto>();
public ObservableList<SpectacleReservationDto> data = FXCollections.observableArrayList();
public Label labelNomSpectacle;
public Button reserverButton;
public void setSpectacleToReserve(SpectacleDto leSpectacle){
//On récupère le spectacle sélectionné
this.spectacleToReserve = leSpectacle;
//On assigne le texte au label
labelNomSpectacle.setText(spectacleToReserve.getNom());
for (TarifDto unTarif:spectacleToReserve.getTarifs()) {
data.add(new SpectacleReservationDto(unTarif.getName(), unTarif.getPrice(), 0));
}
TableColumn nomTarifColumn = new TableColumn("Tarifs");
nomTarifColumn.setCellValueFactory(new PropertyValueFactory<SpectacleReservationDto, String>("tarifName"));
TableColumn prixTarifColumn = new TableColumn("Prix");
prixTarifColumn.setCellValueFactory(new PropertyValueFactory<SpectacleReservationDto, String>("tarifPrice"));
TableColumn nbPlacesReserveColumn = new TableColumn("Nombre de places à réserver");
nbPlacesReserveColumn.setCellValueFactory(new PropertyValueFactory<SpectacleReservationDto, String>("nbPlacesReserve"));
nbPlacesReserveColumn.setCellFactory(TextFieldTableCell.forTableColumn(new NumberStringConverter()));
listeTarifs.setItems(data);
listeTarifs.setEditable(true);
listeTarifs.getColumns().addAll(nomTarifColumn, prixTarifColumn, nbPlacesReserveColumn);
listeTarifs.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
}
public void reserverSpectacle() {
System.out.println("test réserver");
}
public void rafraichirPrixTotal(KeyEvent e) {
if(e.getCode() == KeyCode.ENTER) {
ObservableList<SpectacleReservationDto> lesReservations = listeTarifs.getItems();
for (SpectacleReservationDto uneReservation:lesReservations) {
System.out.println("Prix: " + uneReservation.getTarifPrice());
System.out.println("Nombre de places à réserver: " + uneReservation.getNbPlacesReserve());
}
}
}
}
这是SpectacleReservationDto类:
public class SpectacleReservationDto {
private String tarifName;
private float tarifPrice;
private int nbPlacesReserve;
public SpectacleReservationDto(String tarifName, float tarifPrice, int nbPlacesReserve) {
this.tarifName = tarifName;
this.tarifPrice = tarifPrice;
this.nbPlacesReserve = nbPlacesReserve;
}
public String getTarifName() {
return tarifName;
}
public void setTarifName(String tarifName) {
this.tarifName = tarifName;
}
public int getNbPlacesReserve() {
return nbPlacesReserve;
}
public void setNbPlacesReserve(int nbPlacesReserve) {
this.nbPlacesReserve = nbPlacesReserve;
}
public float getTarifPrice() {
return tarifPrice;
}
public void setTarifPrice(float tarifPrice) {
this.tarifPrice = tarifPrice;
}
}
当我在TableView中编辑值时,会调用rafraichirPrixTotal
函数。
那么我怎样才能获得最后编辑的值?
答案 0 :(得分:0)
只有getter和setter可用,PropertyValueFactory
创建一个不写入项目的属性。要解决这个问题,你可以
使用属性并修复列的类型/使用转换器:
TableColumn<SpectacleReservationDto, Number> nbPlacesReserveColumn = new TableColumn("Nombre de places à réserver");
nbPlacesReserveColumn.setCellValueFactory(new PropertyValueFactory<SpectacleReservationDto, Number>("nbPlacesReserve"));
nbPlacesReserveColumn.setCellFactory(TextFieldTableCell.forTableColumn(new NumberStringConverter(NumberFormat.getIntegerInstance())));
private final IntegerProperty nbPlacesReserve = new SimpleIntegerProperty();
public IntegerProperty nbPlacesReserveProperty() {
retrun nbPlacesReserve;
}
public int getNbPlacesReserve() {
return nbPlacesReserve.get();
}
public void setNbPlacesReserve(int value) {
nbPlacesReserve.set(value);
}
或者为列设置onEditCommit
处理程序:
TableColumn<SpectacleReservationDto, Number> nbPlacesReserveColumn = new TableColumn("Nombre de places à réserver");
nbPlacesReserveColumn.setCellValueFactory(new PropertyValueFactory<SpectacleReservationDto, Number>("nbPlacesReserve"));
nbPlacesReserveColumn.setCellFactory(TextFieldTableCell.forTableColumn(new NumberStringConverter(NumberFormat.getIntegerInstance())));
nbPlacesReserveColumn.setOnEditCommit(evt -> evt.getRowValue().setNbPlacesReserve(evt.getNewValue()));