繁体   English   中英

如何设置和获取JavaFX Table的单元格值(如swing JTable)

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

我是JavaFX新手,并且想知道如何设置和获取JavaFX Table之类的Swing JTable的单元格值。 JavaFX表中Swing JTablesetValueAt()getValueAt的替代方法。

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

}     

TableView确实不支持此方法。

这是使用反射来做您想要的事的一种较脆弱的方法。 它完全取决于您在单元格值工厂中使用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();
    }
}

从JavaFx TableView单元中检索简单值

您可以使用其他文章中列出的侦听器,但是如果您希望从单元格中获取简单值,则可以使用更简单的方法

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

希望本示例对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM