4

TableJavaFXのセル値をのように設定および取得する方法を知りたいのですが、これは初めてです。つまり、表の&の代替。JavaFXSwing JTablesetValueAt()getValueAtSwing JTableJavaFX

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

}     
4

2 に答える 2

3

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();
    }
}
于 2012-11-22T20:31:03.107 に答える
3

JavaFxTableViewセルから単純な値を取得する

他の投稿にリストされているリスナーを使用できますが、セルから単純な値を取得したい場合は、より単純な方法を使用できます

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

この例がお役に立てば幸いです。

于 2013-09-11T18:45:53.930 に答える