0

I try to create a tableview with a different content for a combobox for each row. The user could add an entry to the tableview per button and the object of this entry define the content of the combobox (ObservableList will be changed in background, means it will be filled with new items after clicking a button). I successfully created a combobox for each row, but if I change the items, the content of every combobox in the tableview changes.

    TableColumn<Filterparameter, String> kenntnisgradComboBoxColumn = new TableColumn<Filterparameter, String>("Ausprägung");
    kenntnisgradComboBoxColumn.setCellValueFactory(new PropertyValueFactory<Filterparameter, String>(""));

    Callback<TableColumn<Filterparameter, String>, TableCell<Filterparameter, String>> cellFactoryKenntnisgradComboBoxColumn = new Callback<TableColumn<Filterparameter, String>, TableCell<Filterparameter, String>>()
    {
        @Override
        public TableCell<Filterparameter, String> call(final TableColumn<Filterparameter, String> param)
        {
            final TableCell<Filterparameter, String> cell = new TableCell<Filterparameter, String>()
            {
                // Display combobox if the row is not empty
                @Override
                public void updateItem(String item, boolean empty)
                {
                    super.updateItem(item, empty);

                    if (empty)
                    {
                        setGraphic(null);
                    }
                    else
                    {
                        tableViewkennntisgradComboBox = new ComboBox<Kenntnisgrad>();
                        // Here content of every combobox changes instead of only changing the combobox of selected row 
                        tableViewkennntisgradComboBox.setItems(selectedFilterParameter.getKenntnisgradAuswahl());

                        tableViewkennntisgradComboBox.setPadding(new Insets(-1, 0, -1, 0));
                        tableViewkennntisgradComboBox.setMinWidth(kenntnisgradComboBoxColumn.getWidth() - 6);
                        tableViewkennntisgradComboBox.setPrefWidth(kenntnisgradComboBoxColumn.getWidth() - 6);
                        tableViewkennntisgradComboBox.setMaxWidth(kenntnisgradComboBoxColumn.getWidth() - 6);

                        setGraphic(tableViewkennntisgradComboBox);
                    }
                }
            };

            return cell;
        }
    };

    kenntnisgradComboBoxColumn.setCellFactory(cellFactoryKenntnisgradComboBoxColumn);

Try to visualize the tableview for better understanding:

| TXT   | COMBOBOX     |
| Text1 | [ContentList1 >] |
| Text2 | [ContentList2 >] |
| ........ | [........................] |

Many thanks in advance!

Nighty42
  • 420
  • 1
  • 6
  • 20
  • You need `tableViewkennntisgradComboBox` to be a field in the cell, (so each cell has its own combo box, right now it looks like every cell shares the same one), and it's going to be easiest if the list of values to display is a property of `Filterparameter` (at a minimum, you need a way to map each `Filterparameter` to the list of values to display in the combo box in the corresponding row). – James_D Apr 06 '16 at 11:43
  • Hi James, thanks for your answer. "right now it looks like every cell shares the same one" - think so too. And I also have a property in Filterparameter, it's called "kenntnisgradAuswahl". It's an ObservableList and it's called by "[...].getKenntnisgradAuswahl()" in the posted source code. I found some code from here (http://stackoverflow.com/questions/29387386/populate-combo-box-list-dynamically-for-each-row-in-javafx-table-view) by Uluk Biy but the content of the combobox is set on the editing-Event, not on updateEvent. I unsuccessfully tried to port that to fit my intentions. – Nighty42 Apr 06 '16 at 12:31
  • 1
    So set the combo box items to `getTableView().getItems().get(getIndex()).getKenntnisgradAuswahl()`. (But define a combo box for each cell.) – James_D Apr 06 '16 at 12:43
  • "But define a combo box for each cell." I thought I already did this because I call "tableViewkennntisgradComboBox = new ComboBox();". But apparently it's always the same combobox object.. and I don't know why.. – Nighty42 Apr 06 '16 at 12:56
  • Ah, yeah, you're creating a new one each time `updateItem` is invoked. That's probably too many (performance-wise), but it should actually work. – James_D Apr 06 '16 at 13:00
  • Hmm, now it works. Problem was the line "selectedFilterParameter.getKenntnisgradAuswahl()". I replaced it with your posted line "getTableView().getItems().get(getIndex()).getKenntnisgradAuswahl()" and that seems right. Thanks! Could you write it as answer so I can vote it as answer? – Nighty42 Apr 06 '16 at 13:16
  • I'll add that as an answer – James_D Apr 06 '16 at 13:17

1 Answers1

1

You appear to be setting the items for the combo boxes to those retrieved from some fixed model instance, instead of from the model instance represented by the row in which the cell is displayed.

Replace

tableViewkennntisgradComboBox.setItems(selectedFilterParameter.getKenntnisgradAuswahl());

with

tableViewkennntisgradComboBox.setItems(getTableView().getItems().get(getIndex()).getKenntnisgradAuswahl());
James_D
  • 201,275
  • 16
  • 291
  • 322