JavaFX TableView 如何获取单元格的数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29090583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaFX TableView how to get cell's data?
提问by sashaaero
I'm using that method to get whole object.
我正在使用该方法来获取整个对象。
tableView.getSelectionModel().getSelectedItem()
How can I get data from single cell?
I mean like get S20BJ9DZ300266as String?
如何从单个单元格获取数据?
我的意思是像获取S20BJ9DZ300266作为字符串?
采纳答案by James_D
Assuming you know something is selected, you can do
假设你知道某些东西被选中,你可以做
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();
回答by Rabie Khoualdia
table.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == null) {
selected.setText("");
return;
}
System.out.println(newValue.getId());
});
NB: "getId" is your "get" Column and "table" is the name of your tabelview
注意:“getId”是您的“get”列,“table”是您的 tabelview 的名称
回答by Nicolas Angelico
I solved it. I get the string, for example, "[1, proveedor1, calzado1, Rojo, 39, 600, 2, 1200]", and get the frist cell (id) whit substring. saludos
我解决了。我得到字符串,例如,“[1,provedor1, calzado1, Rojo, 39, 600, 2, 1200]”,并得到第一个单元格 (id) whit 子字符串。问候语
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);
回答by Reuben The Coder
If you are using scene builder, add a method to On Edit Commit for the particular column and add the logic in the controller class. Use event.getNewValue()
to get the new value entered by the user. Eg.
如果您使用的是场景构建器,请为特定列的 On Edit Commit 添加一个方法,并在控制器类中添加逻辑。使用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());
}
Use this when you allow editable columns
当您允许可编辑的列时使用此选项
回答by Howard J
Assuming you haven't selected any row but know where and what you want....
假设您没有选择任何行,但知道您想要的位置和内容......
// 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);
回答by Samer
you can use this code i think it's working
您可以使用此代码,我认为它有效
String a=personTable.getColumns().get(0).getCellObservableValue(0).getValue().toString();
System.out.println("value"+a);