如何在javafx中获取tablecolumn的每个单元格的值并存储在变量中

时间:2016-10-21 11:44:16

标签: mysql javafx

我没有得到变量中tablecolumn的每个表格单元格的值。 如何获得tableview的每个单元格的值?

我尝试使用此代码获取单元格值,但它不起作用,我收到错误。

    int mappingid = Integer.parseInt(ocolFileOutputMappingId.getText());
    int srno = Integer.parseInt(ocolFileSrno.getText());
    String filedname = ocolFieldName.getText();

我的完整代码是:

 @FXML
    private TableColumn<InputMappingFieldModel, Integer> ocolFileSrno;// for srno

    @FXML
    private TableColumn<InputMappingFieldModel, Integer> ocolFileOutputMappingId;//thisfor output mapping id
    @FXML
    private TableColumn<InputMappingFieldModel, String> ocolFieldName;


    upaction.setOnAction(evt -> {
    int index = otableview.getSelectionModel().getSelectedInde);
    // swap items
    otableview.getItems().add(index - 1, otableview.getItems().remove(index));
    // select item at new position
    otableview.getSelectionModel().clearAndSelect(index - 1);

    // code to insert shuffle data in table
    try {
    int txtmapoutputid = Integer.parseInt(outputIdText.getText());
    String sql = "update OUTPUT_MAPPING set SERIAL_NUMBER=?,FIELD_NAME=?,OUTPUT_MAPPING_ID=? where OUTPUT_ID="+txtmapoutputid;
    // System.out.println("SQL IS mapping Update "+txtmapinputid);
    PreparedStatement psmt = conn.prepareStatement(sql);
    int mappingid = Integer.parseInt(ocolFileOutputMappingId.getText());
    int srno = Integer.parseInt(ocolFileSrno.getText());
    String filedname = ocolFieldName.getText();
    psmt.setInt(1, srno);
    psmt.setString(2, filedname);
    psmt.setInt(3, mappingid);

    psmt.executeUpdate();
    } catch (Exception ex) {
    ex.printStackTrace();
    }

    otableview.getColumns().setAll(ocolFileSrno, ocolFieldName, col_spaceswap, ocolFileOutputMappingId);//remove actionColUpdate from parameter list 
    otableview.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
    otableview.setItems(modelViewField);
    otableview.refresh();
    });

有谁可以向我提供如何在tableview中获取表列的单元格值的逻辑?

1 个答案:

答案 0 :(得分:0)

简短的回答是,您没有从单元格中获取值,您可以从表格的项目列表中获取值:

int index = otableview.getSelectionModel().getSelectedIndex();
InputMappingFieldModel item = otableview.getItems().get(index);

// or just
// InputMappingFieldModel item = otableview.getSelectionModel().getSelectedItem();

// this assumes you are using mappingId as the property for ocolFileOutputMappingId:
int mappingId = item.getMappingId();

// and assuming you are using serNo as the property for ocolFieldName
int serNo = item.getSerNo();

// ...