How to set and get the cell value of JavaFX Table

2019-04-09 12:31发布

I am new to JavaFX and would like to know how to set and get the cell value of JavaFX Table like Swing JTable. i.e. an alternative of setValueAt() & getValueAt of Swing JTable in JavaFX Table.

//would like to get the index of column by Name    
int index_RunnableQueueItem = tableQueue.getColumns().indexOf("RunnableQueueItem");
//would like to get the selected row
int row = tableQueue.getSelectionModel().getSelectedIndex();
if (index_RunnableQueueItem != -1 && row != -1) {
// would like to get the value at index of row and column.

//Update that value and set back to cell. 

}     

标签: java javafx-2
2条回答
趁早两清
2楼-- · 2019-04-09 13:13

TableView really doesn't support this methodology.

Here's a somewhat brittle means of doing what you want, using reflection. It's entirely dependent upon you using PropertyValueFactory in your cell value factory so it can lookup the property name, though.

class MyItem
{
    SimpleStringProperty nameProperty = new SimpleStringProperty("name");
    public MyItem(String name) {
        nameProperty.set(name);
    }
    public String getName() { return nameProperty.get(); }
    public void setName(String name) { nameProperty.set(name); }
    public SimpleStringProperty getNameProperty() { return nameProperty; }
}

...

TableView<MyItem> t = new TableView<MyItem>();
TableColumn col = new TableColumn("Name Header");
col.setCellValueFactory(new PropertyValueFactory<MyItem, String>("name"));
t.getColumns().addAll(t);

...

public void setValue(int row, int col, Object val)
{
    final MyItem selectedRow = t.getItems().get(row);
    final TableColumn<MyItem,?> selectedColumn = t.getColumns().get(col);
    // Lookup the propery name for this column
    final String propertyName =   ((PropertyValueFactory)selectedColumn.getCellValueFactory()).getProperty();
    try 
    {
        // Use reflection to get the property
        final Field f = MyItem.class.getField(propertyName);
        final Object o = f.get(selectedRow);

        // Modify the value based on the type of property
        if (o instanceof SimpleStringProperty)
        {
            ((SimpleStringProperty)o).setValue(val.toString());
        }
        else if (o instanceof SimpleIntegerProperty)
        {
            ...
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
查看更多
男人必须洒脱
3楼-- · 2019-04-09 13:29

Retrieving a simple value from a JavaFx TableView Cell

You can use listeners as listed in other posts, but if you wish to get just a simple value from a cell you can use a simpler method

Example:

// Get the row index where your value is stored
    int rowIndex = tableView.getSelectionModel().getSelectedIndex();

// Since you can't get the value directly from the table like the 
// getValueAt method in JTable, you need to retrieve the entire row in
// FXCollections ObservableList object
    ObservableList rowList = 
        (ObservableList) tableViewModelos.getItems().get(rowIndex);

// Now you have an ObservableList object where you can retrieve any value
// you have stored using the columnIndex you now your value is, starting
// indexes at 0;

// In my case, I want to retrieve the first value corresponding to the first column           //index, and I know it is an Integer Value so I'll cast the object.
int columnIndex = 0;
int value = Integer.parseInt(rowList.get(columnIndex).toString());

Hope this expample helps you.

查看更多
登录 后发表回答