编辑用hibernate填充的javafx tableview

时间:2017-03-30 10:54:25

标签: java hibernate javafx tableview

我在我的应用程序中创建了一个tableview,它使用Hibernate显示来自derbydb的值。它工作正常,现在我想使表格可编辑,但我不明白如何继续;我尝试添加setOnEditCommit ....但它不起作用,我该怎么办?

@FXML protected void cercaProd(ActionEvent event) {
    magaz_table.getItems().clear();
    magaz_table.setEditable(true);
    Configuration configuration = new Configuration().configure();
    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
    SessionFactory factory = configuration.buildSessionFactory(builder.build());
    Session session = factory.openSession();
    session.beginTransaction();
    String cercanome=txtfield_cerca_prod.getText();
    String sQuery = "from Magazzino WHERE ITEM_NOME=:nomeprod";
    Query q3 = session.createQuery(sQuery);
    q3.setParameter("nomeprod", txtfield_cerca_prod.getText());
        List result = q3.list();
        Iterator iterator = result.iterator();
        while (iterator.hasNext()) {
              Magazzino obj = (Magazzino)iterator.next();
              data.add(obj);
        }
    itemIdCol.setCellValueFactory(new PropertyValueFactory<Magazzino, Integer>("itemId"));             
    NomeCol.setCellValueFactory(new PropertyValueFactory<Magazzino, String>("itemNome"));
    NomeCol.setCellFactory(TextFieldTableCell.forTableColumn());
    NomeCol.setOnEditCommit((CellEditEvent<Magazzino, String> t) -> {
            ((Magazzino) t.getTableView().getItems().get(t.getTablePosition().getRow())).setItemNome(t.getNewValue());        
    });
    magaz_table.setItems(data);    
    session.getTransaction().commit();
    session.close();
}

fxml的一部分

<TableView fx:id="magaz_table" editable="true" layoutX="1.0" layoutY="37.0" prefHeight="407.0" prefWidth="800.0">
      <columns>
           <TableColumn fx:id="itemIdCol" prefWidth="75.0" text="Id" />
           <TableColumn fx:id="NomeCol" editable="true" prefWidth="75.0" text="Nome" />
      </columns>
</TableView>         

0 个答案:

没有答案