java 如何像swing JTable一样设置和获取JavaFX Table的单元格值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13512755/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:03:07  来源:igfitidea点击:

How to set and get the cell value of JavaFX Table like swing JTable

javajavafx-2

提问by Ashish Pancholi

I am new to JavaFXand would like to know how to set and get the cell value of JavaFXTable like SwingJTable. i.e. an alternative of setValueAt()& getValueAtof SwingJTablein JavaFXTable.

我是新手JavaFX,想知道如何设置和获取JavaFXTable的单元格值,例如SwingJTable. 即的替代setValueAt()getValueAtSwingJTableJavaFX表中。

//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. 

}     

采纳答案by jhsheets

TableView really doesn't support this methodology.

TableView 确实不支持这种方法。

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.

这是使用反射做你想做的事情的一种有点脆弱的方法。不过,这完全取决于您在单元格值工厂中使用 PropertyValueFactory 是否可以查找属性名称。

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

回答by José Roberto Ramírez Aguilar

Retrieving a simple value from a JavaFx TableView Cell

从 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.

希望这个例子对你有帮助。