I'm trying to create an editable table using javaFX Scene builder, but I can not get it to fire the On Edit Commit event, after editing the cell (ending with enter input), the input box turns blank but still covers the cell and remains until I close it by selecting it and pressing esc.

The code for my controller as as following:

public class PlaylistsWindowController extends AbstractController implements EventHandler<WindowEvent> {

    private Set<Song> libary;
    private final ObservableList<SongWraper> libaryTableData = FXCollections.observableArrayList();
    private final String LIBADRESS = "data\\libary.sav";

    @FXML
    TableColumn<SongWraper, String> titleColumn;

    @FXML
    TableColumn<SongWraper, String> artistColumn;

    @FXML
    TableColumn<SongWraper, String> genreColumn;

    @FXML
    TableColumn<SongWraper, String> serieColumn;

    @FXML
    TableColumn<SongWraper, Integer> pointGainColumn;

    @FXML
    TableColumn<SongWraper, Integer> pointLossColumn;

    @FXML
    TableView<SongWraper> libaryTable;

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {

        /*Code creating the libaryTableData*/


        configLibaryTable();
    }

    private void configLibaryTable(){
        titleColumn.setCellValueFactory(new PropertyValueFactory<SongWraper, String>("title"));
        artistColumn.setCellValueFactory(new PropertyValueFactory<SongWraper, String>("artistName"));
        serieColumn.setCellValueFactory(new PropertyValueFactory<SongWraper, String>("serieName"));
        genreColumn.setCellValueFactory(new PropertyValueFactory<SongWraper, String>("genre"));
        pointGainColumn.setCellValueFactory(new PropertyValueFactory<SongWraper, Integer>("pointGain"));
        pointLossColumn.setCellValueFactory(new PropertyValueFactory<SongWraper, Integer>("pointLossColumn"));
        libaryTable.setItems(libaryTableData);
        libaryTable.setEditable(true);
        libaryTableEditCommit();
    }

    private void libaryTableEditCommit(){
        titleColumn.setCellFactory(TextFieldTableCell.<SongWraper>forTableColumn());
        titleColumn.setOnEditCommit(
                new EventHandler<TableColumn.CellEditEvent<SongWraper, String>>(){
                    @Override
                    public void handle(CellEditEvent<SongWraper, String> t){
                        ((SongWraper) t.getTableView().getItems().get(t.getTablePosition().getRow())
                                ).setTitle(t.getNewValue());
                    }
                }
                );
    }

And the FXML:

<AnchorPane prefHeight="600.0" prefWidth="800.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="gui.PlaylistsWindowController">
  <!-- TODO Add Nodes -->
                  -------unreleated code-------
                    <TableView id="table" fx:id="libaryTable" editable="true" prefHeight="272.0" prefWidth="494.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="24.0">
                      <columns>
                        <TableColumn prefWidth="75.0" text="Title" fx:id="titleColumn" />
                        <TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="91.0" text="Artist" fx:id="artistColumn" />
                        <TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="91.0" text="Serie" fx:id="serieColumn" />
                        <TableColumn prefWidth="75.0" text="Genre" fx:id="genreColumn" />
                        <TableColumn prefWidth="75.0" text="Point Gain" fx:id="pointGainColumn" />
                        <TableColumn prefWidth="75.0" text="Point Lost" fx:id="pointLossColumn" />
                      </columns>
                    </TableView>
                  -------unreleated code-------
</AnchorPane>

Anyone know the reason for this problem and how to fix it?

有帮助吗?

解决方案

If your model class (SongWraper) (SongWrapper? SongRapper...? ;)) has JavaFX-style property methods (i.e.

public class SongWraper {
  private final StringProperty title = new SimpleStringProperty(this, "title", "");
  public final String getTitle() {
    return title.get();
  }
  public final void setTitle(String title) {
    this.title.set(title);
  }
  public final StringProperty titleProperty() {
    return title ;
  }
}

then the onEditCommit shouldn't be necessary. The TextFieldTableCell binds its text property to the appropriate property in the model class. I think, because of this, the TextFieldTableCell doesn't fire the onEditCommit, though I am not sure on that last point.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top