0

我遇到了一些问题,需要指导!我有一个具有 2 个 ComboBoxTableCells 的 TableView,我的要求是在第一个单元格更改时更新第二个单元格的组合框中的列表。

我已经尝试过以下方式,到目前为止还没有运气。

 public class Test{
    private StringProperty name;
    private StringProperty city;

    public Test(String name, String city){
        this.name = new SimpleStringProperty(name);
        this.city = new SimpleStringProperty(city);
    }

    public String getName() {
        return name.get();
    }

    public void setName(String name) {
        this.name.setValue(name); 
    }

    public String getCity() {
        return city.get();
    }

    public void setCity(String city) {
        this.city.setValue(city);
    }

    public StringProperty nameProperty() {return name;}
    public StringProperty cityProperty() {return city;}
}


    TableView _table= new TableView();
    final ObservableList list = FXCollections.observableArrayList();
    list.add("name 1");
    list.add("name 2");
    list.add("name 3");
    list.add("name 4");

    final ObservableList list2 = FXCollections.observableArrayList();
    list2.add("city 1");
    list2.add("city 2");
    list2.add("city 3");
    list2.add("city 4");

    TableColumn firstNameCol = new TableColumn("First Name");
    firstNameCol.setMinWidth(100);
    firstNameCol.setCellValueFactory(new PropertyValueFactory<Test, String>("name"));
    firstNameCol.setCellFactory(ComboBoxTableCell.forTableColumn(list));

    firstNameCol.setOnEditCommit(
        new EventHandler<CellEditEvent<Test, String>>() {
            @Override
            public void handle(CellEditEvent<Test, String> t) {
     ((Test) t.getTableView().getItems().get(t.getTablePosition().getRow())).setName(t.getNewValue());

                System.out.println(t.getTableColumn().getCellData(t.getTablePosition().getRow()));

我想必须在这里做点什么,尝试以下行来查看对各个单元格
list2.clear(); 的影响;
它更新了整个列的数据,我只希望它仅针对相应的单元格进行更新。

            }
        }
    );


    TableColumn lastNameCol = new TableColumn("City");
    lastNameCol.setMinWidth(100);
    lastNameCol.setCellValueFactory(
        new PropertyValueFactory<Test, String>("city"));
    lastNameCol.setCellFactory(ComboBoxTableCell.forTableColumn(list2));
    lastNameCol.setOnEditCommit(
        new EventHandler<CellEditEvent<Test, String>>() {
            @Override
            public void handle(CellEditEvent<Test, String> t) {
                ((Test) t.getTableView().getItems().get(
                        t.getTablePosition().getRow())
                        ).setName(t.getNewValue());
            }
        }
    );

    _table.setEditable(true);
    _table.getColumns().addAll(firstNameCol,lastNameCol);

    ObservableList listItems = FXCollections.observableArrayList();
    listItems.add(new Test("name 4", "city 2"));
    listItems.add(new Test("name 2", "city 3"));
    table.getTableView().setItems(listItems);
    _table.setItems(listItems); 

任何帮助将不胜感激。谢谢

4

1 回答 1

1

这是一个我没有测试过的hacky方法:

  1. 在您将用于在 firstNameCol 和 lastNameCol 之间进行通信的数据项上添加一个虚拟(布尔值?)属性
  2. 在 firstNameCol 的 onEditCommit 处理程序中,更改虚拟属性的值。确保它发生变化。
  3. 让 lastNameCol 成为虚拟属性的列。为 lastNameCol 注册一个单元工厂,它返回一个 TableCell 并覆盖了 updateItem() 方法(下面的伪代码)

    lastNameCol.setCellFactory(new Callback() {
        @Override
        public TableCell call(TableColumn col) {
            return new TableCell() {
                @Override
                public void updateItem(Boolean item, boolean empty) {
                    if (!empty) {
                        // Don't care about the value of item
                        // Just look up the value of firstNameCol using
                        // getTablePosition(), then create and populate
                        // a ComboBox with the appropriate items and set
                        // it as the graphic for this cell via this.setGraphic()
                        // Add handler to ComboBox control to update data item when
                        // selection changes
                    }                                                
                }
            };
        }
    });
    
于 2013-07-22T18:58:16.563 回答