如何更新TableView中的ObservableList以写入DB

时间:2015-10-05 02:06:03

标签: javafx tableview fxml

当我的数据库信息导入TableView时,一切都正确显示但不可编辑,我无法弄清楚导致这种情况的原因。谁能发现问题?

    @FXML
public TableView<Clients> activeclients;

@FXML
public void databaseConnection(ActionEvent event) {
    Connection c = null;

    ObservableList data = FXCollections.observableArrayList();

    try {
        c = DriverManager.getConnection("jdbc:sqlite:db/clients.db");
        c.setAutoCommit(false);
        System.out.println("Opened Database Successfully");
        String SQL = "SELECT * from clientstest";

        //ResultSet
        ResultSet rs = c.createStatement().executeQuery(SQL);

        /**
         * ********************************
         * TABLE COLUMN ADDED DYNAMICALLY * ********************************
         */
        for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
            //We are using non property style for making dynamic table
            final int j = i;

            TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i + 1));
            col.setCellFactory(TextFieldTableCell.<Clients>forTableColumn());

col.setOnEditCommit(e -> {
                ObservableList row = e.getRowValue();
                row.set(j, e.getNewValue());
            });

            col.setCellValueFactory(new Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {

                @Override
                public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
                    return new SimpleStringProperty(param.getValue().get(j).toString());
                }
            });
            col.setMinWidth(175);

            activeclients.getColumns().addAll(col);
            System.out.println("Column [" + i + "] ");
            col.setEditable(true);
            activeclients.setEditable(true);
        }

        /**
         * ******************************
         * Data added to ObservableList * ******************************
         */
        while (rs.next()) {
            //Iterate Row
            ObservableList<String> row = FXCollections.observableArrayList();
            for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                //Iterate Column
                row.add(rs.getString(i));
            }
            System.out.println("Row [1] added " + row);
            data.add(row);

        }

        //FINALLY ADDED TO TABLEVIEW  
        activeclients.setItems(data);
        activeclients.setEditable(true);

    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        System.exit(0);
    }

}

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}

}

客户CLASS

包mach1jedi.model;

导入javafx.beans.property.SimpleStringProperty;

公共类客户{{/ p>

private SimpleStringProperty fname;
private SimpleStringProperty lname;
private SimpleStringProperty clientID;
private SimpleStringProperty provcontractnum;
private SimpleStringProperty adscode;
private SimpleStringProperty transcode;
private SimpleStringProperty ccucontractnum;

public SimpleStringProperty getFname() {
    return fname;
}

public SimpleStringProperty getLname() {
    return lname;
}

public SimpleStringProperty getClientID() {
    return clientID;
}

public SimpleStringProperty getProvcontractnum() {
    return provcontractnum;
}

public SimpleStringProperty getAdscode() {
    return adscode;
}

public SimpleStringProperty getTranscode() {
    return transcode;
}

public SimpleStringProperty getCcucontractnum() {
    return ccucontractnum;
}

public void setFname(SimpleStringProperty fname) {
    this.fname = fname;
}

public void setLname(SimpleStringProperty lname) {
    this.lname = lname;
}

public void setClientID(SimpleStringProperty clientID) {
    this.clientID = clientID;
}

public void setProvcontractnum(SimpleStringProperty provcontractnum) {
    this.provcontractnum = provcontractnum;
}

public void setAdscode(SimpleStringProperty adscode) {
    this.adscode = adscode;
}

public void setTranscode(SimpleStringProperty transcode) {
    this.transcode = transcode;
}

public void setCcucontractnum(SimpleStringProperty ccucontractnum) {
    this.ccucontractnum = ccucontractnum;
}

}

1 个答案:

答案 0 :(得分:1)

您没有在表格列上设置cellFactory。默认单元工厂提供了一个不支持编辑的单元格(基本上单元格只是一个标签)。你需要

col.setCellFactory(TextFieldTableCell.forTableColumn());

将为列提供基于TextField的编辑单元格。

要使ObservableList<ObservableList>更新,因为您的表不使用标准的JavaFX属性模式,您还需要为编辑提交注册一个处理程序:

col.setOnEditCommit(e -> {
    ObservableList row = e.getRowValue();
    row.set(j, e.getNewValue());
});

请注意,您在代码中使用了大量原始类型,您应该从编译器中获取大量警告。您可能需要修复这些以使用正确的类型,以便获取上面的代码进行编译。 (例如,你应该有TableView<ObservableList<String>>TableColumn<ObservableList<String>, String>等等。)