Adding values to selected javafx ComboBoxTableCell dynamically

[亡魂溺海] 提交于 2020-01-21 08:57:06

问题


I have a TableView with two columns named Product and Brand. A Product can be of different Brands. Eg. TV has different brands like Samsung, Sony etc. I am showing Brands of a Product in ComboBoxes.

This is how i am adding a ComboBoxTableCell for my Brand Column.

    ObservableList<String> catList = FXCollections.observableArrayList();

    categoryCol.setCellFactory(t -> {   
        ComboBoxTableCell comboCell = new ComboBoxTableCell(catList);
        return comboCell;
    });
    contactTable.getColumns().add(categoryCol);

Now in these ComboBoxes i want to add values of Brands. Since There will be different Products so their Brands will be different also. Eg.

Product  |  Model
----------------------------------------------
TV       |  ComboBox[Samsung, Sony, Panasonic] 
Monitor  |  ComboBox[Dell, Microsoft, Apple ]

Now since the ComboBoxes have same data model (ObservableList) how can i add different values to them by selecting the items in table. Is it possible to do ? Thanks in advance for any help.


回答1:


First of all you need a custom row class in which you store the elements, then you have to @Override the startEdit() from theComboBoxTreeTableCell for example this way :

@Override public void startEdit() {
    MyCustomRow currentRow = getTableRow().getItem();
    getItems().setAll(currentRow.getModels());
        super.startEdit();
    }
}

MyCustomRow:

package mypackage;

import javafx.beans.property.SimpleStringProperty;

import java.util.List;
import java.util.Map;

public class MyCustomRow {

    private SimpleStringProperty product;

    private SimpleStringProperty model;

    private List<String> allModels;

    public MyCustomRow(
            String product,
            String model,
            List<String> models) {
        this.product = new SimpleStringProperty(product);
        this.model = new SimpleStringProperty(product);
        this.allModels = models;
   }

    public String getProduct() {
        return product.get();
    }

    public SimpleStringProperty productProperty() {
        return product;
    }

    public String getModel() {
        return model.get();
    }

    public SimpleStringProperty modelProperty() {
        return model;
    }

    public List<String> getModels() {
        return allModels;
    }
}

Then in your contoller class you can say:

ObservableList<String> carList = FXCollections.observableArrayList();

    categoryCol.setCellFactory(t -> new ComboBoxTableCell(carList){
         @Override public void startEdit() {
            MyCustomRow currentRow = getTableRow().getItem();
            getItems().setAll(currentRow.getModels());
            super.startEdit();
        }
    });
    categoryCol.setCellValueFactory(v -> v.getValue().modelProperty());
    contactTable.getColumns().add(categoryCol);

So each row you add the appropriate models. So in the ComboBox you will have only those items(models) which belong to the product



来源:https://stackoverflow.com/questions/44319133/adding-values-to-selected-javafx-comboboxtablecell-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!