tableCoulmn不会触发编辑提交

时间:2014-01-28 21:47:40

标签: javafx-2 fxml scenebuilder

我正在尝试使用javaFX场景构建器创建一个可编辑的表,但我无法在编辑单元格(以输入输入结束)后触发On Edit Commit事件,输入框变为空白但仍然覆盖单元格并保持不变,直到我通过选择它并按下esc来关闭它。

我的控制器代码如下:

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());
                    }
                }
                );
    }

和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>

任何人都知道这个问题的原因以及如何解决它?

1 个答案:

答案 0 :(得分:0)

如果你的模型类(SongWraper)(SongWrapper?SongRapper ......?;))具有JavaFX风格的属性方法(即

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 ;
  }
}

然后onEditCommit不一定是必要的。 TextFieldTableCell将其text属性绑定到模型类中的相应属性。我想,因为这样,TextFieldTableCell不会触发onEditCommit,虽然我不确定最后一点。