【问题标题】:How to Add Button Edit and delete in JFXTable View and How to edit row to the SQL如何在 JFXTable 视图中添加按钮编辑和删除以及如何将行编辑到 SQL
【发布时间】:2018-05-10 14:33:07
【问题描述】:

大家好,我有两个问题! 我正在为 gui 使用 NetBeans 8.2 和 SceneBuilder

有一个来自 SQL 的数据的 JFXTableView 字段我想添加一列操作包含两个按钮,每行的编辑和删除

我想在我的 sql 中编辑行和更新数据,我用它来更新,但我不知道如何实现它们

 public static void modifierElement(int id, String nom, int prix, int qnt) {
    try {
        String query = "UPDATE element SET element='" + nom
                + "', prix=" + prix
                + ", quantite=" + qnt
                + " WHERE id=" + id;
        cnx = connecterDB();
        st = cnx.createStatement();
        st.executeUpdate(query);
        System.out.println("Produit bien modifié");

    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }

}

public static Connection connecterDB() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        //System.out.println("Driver oki");
        String url = "jdbc:mysql://127.0.0.1:3306/taxiphone";
        String user = "root";
        String password = "";
        Connection cnx = DriverManager.getConnection(url, user, password);
        //System.out.println("Connexion bien établié");
        return cnx;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

我试过用这个

TableElement.setEditable(true);
    clmID.setCellFactory(TextFieldTableCell.forTableColumn());
    clmELement.setCellFactory(TextFieldTableCell.forTableColumn());
    clmPrix.setCellFactory(TextFieldTableCell.forTableColumn());
    clmQuantite.setCellFactory(TextFieldTableCell.forTableColumn());

它只是改变了它没有保存在我的 SQL 数据库中的实例,谢谢你们

【问题讨论】:

    标签: mysql sql javafx sql-update tableview


    【解决方案1】:

    对于操作列,您可以使用HBox(包含删除和更新按钮)填充每个单元格。对于HBox中的每个按钮,您可以设置监听器并相应地执行数据库操作

    actionCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Person,HBox>, ObservableValue<HBox>>() {
    
            @Override
            public ObservableValue<HBox> call(CellDataFeatures<Person, HBox> currentRow)
            {
                Button updateButton = new Button("update");
                Button deleteButton = new Button("delete");
    
                updateButton.setOnAction(e->{
                    //Database operation to update record goes here
                });
    
                deleteButton.setOnAction(e->{
                    //Database operation to deleted record goes here                
                });
    
    
    
                ObservableValue<HBox> s = new SimpleObjectProperty<>(new HBox(updateButton,deleteButton));
    
    
                return s;
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-12
      • 1970-01-01
      • 2019-06-09
      • 1970-01-01
      相关资源
      最近更新 更多