3

Thare 是 javaFX 文档页面上的教程。这个例子描述了如何制作 tableView,如果你有一些 java 类,它可以告诉你你将拥有哪些列。(在这个例子中是一个 Person 类)。

但是如果我没有任何特定的类,并且列数可能会不时变化怎么办?就我而言,我有这样的数据结构:

class TableData{ 
    List<Row> rows; //A list with all my rows i need to have in my table
}

class Row{
    List<Column> columns; //Cells\Columns for every row. 
}

class Column{
    Attribute attr; //Each column - is somethig like a wrapper for the real data i need to show in a cell;
}

class Attribute{ //My precues data
    String name;
    SupportingInfo info;
}

class SupportingInfo{//Some supporting fields...
    String name;
    String value;
    //...etc....
}

所以,我的情况与这个非常相似。唯一不同的是,上述案例中的数据并未与其在 javaFX 表中的表示绑定(因此,即使有人会制作额外的控件来在 tableView 中编辑此数据,具有该数据的实际对象也永远不会知道它。 ),因为它(数据)像一些字符串一样进入表格,而不是像一些对象;

所以,我需要什么 - 将数据推送到表中(例如:table.setItems(tableData)),设置一些集合工厂,让用户能够编辑数据,并将编辑后的数据放在我的 tableData 对象中;

以下是我为此目的尝试制作的一些代码:

//prepare my table
private void createTableHeader(TableView table, List<Attribute> ias) {
    int i = 0;
    for (final Attribute ia : ias) {
        final int j = i;
        i++;
        TableColumn tc = new TableColumn(ia.getName());
        tc.setSortable(true);
        tc.setCellValueFactory(new Callback<CellDataFeatures<List<Attribute>, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(CellDataFeatures<List<Attribute>, String> arg0) {
                if(arg0.getValue().get(j).getSupportingInfo() == null){
                    arg0.getValue().get(j).setSupportingInfo(new SupportingInfo());
                }
                return new SimpleObjectProperty(arg0.getValue().get(j),"value");
            }
        });
        table.getColumns().add(tc);
    }
}

//loading some data to my tableView
private void createTableBody(TableView curTable, List<Row> rows) {  
    ObservableList<List<Attribute>> data = FXCollections.observableArrayList();
    for (Row row : rows) {
        data.add(row.getColumns());
    }
    curTable.setItems(data);
}   

//this one is to define some extra controls for editing data in a table by users
private void makeCellFactory(TableColumn curTableCol, final Attribute templateIa, final Document doc) {
    curTableCol.setCellFactory(new Callback<TableColumn, TableCell>() {
        public TableCell call(TableColumn p) {
            final EditingCell cell = new EditingCell(templateIa, doc);
            return cell;
        }
    });
}       

但是,结果,我的表​​格中只有空行,能够单击某些单元格并接收表格编辑控件。但是按表中没有默认值;我在我的代码中做错了什么?

4

1 回答 1

1

好的,我找到了解决方案: ts.setCellFactory 应该如下所示:

tc.setCellValueFactory(new Callback<CellDataFeatures<List<Attribute>, SupportingInfo>, ObservableValue<Attribute>>() {
 @Override
 public ObservableValue<Attribute> call(CellDataFeatures<List<Attribute>, SupportingInfo> arg0) {
  return new SimpleObjectProperty(arg0.getValue().get(j),"value",arg0.getValue().get(j));
 }
});

此外,需要此代码来捕获新值并将传入数据放入表中:

tc.setOnEditCommit(new EventHandler<CellEditEvent<List<Attribute>, Attribute>>() {
    @Override
    public void handle(CellEditEvent<List<Attribute>, Attribute> t) { t.getTableView().getItems().get(t.getTablePosition().getRow()).get(j).setSupportingInfo(t.getNewValue().getSupportingInfo());
    }
});
于 2012-09-30T08:12:45.137 回答