JavaFX TableView如何获取单元格的数据?

时间:2015-03-17 02:59:22

标签: java javafx tableview

我使用该方法获取整个对象。

tableView.getSelectionModel().getSelectedItem()

如何从单个细胞中获取数据? enter image description here
我的意思是像 S20BJ9DZ300266 一样串?

6 个答案:

答案 0 :(得分:11)

假设您知道选择了某些内容,则可以

TablePosition pos = table.getSelectionModel().getSelectedCells().get(0);
int row = pos.getRow();

// Item here is the table view type:
Item item = table.getItems().get(row);

TableColumn col = pos.getTableColumn();

// this gives the value in the selected cell:
String data = (String) col.getCellObservableValue(item).getValue();

答案 1 :(得分:0)

table.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue == null) {
                selected.setText("");
                return;
            }
                       System.out.println(newValue.getId());
        });

注意:" getId"是你的"得到"专栏和"表"是您的tabelview的名称

答案 2 :(得分:0)

我解决了。我得到了字符串,例如,“[1,proveedor1,calzado1,Rojo,39,600,2,1200]”,并获得frist cell(id)whit substring。 saludos

TablePosition pos = (TablePosition) tableVenta.getSelectionModel().getSelectedCells().get(0);
int index = pos.getRow();
String selected = tableVenta.getItems().get(index).toString();
selected = selected.substring(1, selected.indexOf(","));
System.out.println(selected);

答案 3 :(得分:0)

如果使用的是场景生成器,则将“方法”添加到特定列的“编辑时提交”,然后在控制器类中添加逻辑。使用event.getNewValue()获取用户输入的新值。例如

@FXML
private void UpdateName(TableColumn.CellEditEvent<ModelClass, String> event) {
    ModelClass mc = Table.getSelectionModel().getSelectedItem();
    mc.setName(event.getNewValue());
    System.err.println("new value "+event.getNewValue());
}

当您允许可编辑的列时使用此

答案 4 :(得分:0)

假设您没有选择任何行,但是知道您想要的位置和内容。

// Item here is the table view type:
    Unpaid item = userTable.getItems().get(0);

    TableColumn col = userTable.getColumns().get(3);

    // this gives the value in the selected cell:
    String data = (String) col.getCellObservableValue(item).getValue();
    JOptionPane.showMessageDialog(null, data);

答案 5 :(得分:0)

您可以使用此代码,我认为它可以正常工作

String a=personTable.getColumns().get(0).getCellObservableValue(0).getValue().toString(); 
System.out.println("value"+a);
相关问题