繁体   English   中英

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

[英]How to get the value of each cell of tablecolumn in javafx and store in variable

我没有在变量中获取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中获取表列的单元格值的逻辑?

简短的答案是,您没有从单元格中获取值,而是从表的项目列表中获取了值:

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

// ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM