sashaaero
sashaaero

Reputation: 2908

JavaFX TableView how to get cell's data?

I'm using that method to get whole object.

tableView.getSelectionModel().getSelectedItem()

How can I get data from single cell? enter image description here
I mean like get S20BJ9DZ300266 as String?

Upvotes: 12

Views: 46672

Answers (7)

Seco
Seco

Reputation: 21

I'm using something like this to add data to table:

ObservableList<Artikl> data = FXCollections.observableArrayList();
data.add(new Artikl("Some String data"));

Artikl is class of setters and getters.

public class Artikl {
private String KAT;
public Artikl(String kb)
{
   this.KAT=kb;
}

public String getKAT()
{
    return KAT;
}
public void setKAT(String kb)
{
    KAT=kb;
}
}

And to get it back I'm using:

ObservableList<Artikl> data = tableName.getItems();
for(int i=0;i<data.size();i++)
{
System.out.println(data.get(i).getKAT());
}

This prints it all. To get selected item:

System.out.println(data.get(tableName.getSelectionModel().getSelectedIndex()).getKAT());

Upvotes: 0

Samer
Samer

Reputation: 161

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

Upvotes: 0

Howard J
Howard J

Reputation: 421

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

Upvotes: 4

Reuben Frimpong
Reuben Frimpong

Reputation: 93

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.

@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

Upvotes: 0

Nicolas Angelico
Nicolas Angelico

Reputation: 1

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

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

Upvotes: 0

Rabie Khoualdia
Rabie Khoualdia

Reputation: 1

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

Upvotes: 0

James_D
James_D

Reputation: 209714

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

Upvotes: 20

Related Questions