1 Reply Latest reply: Oct 12, 2013 8:24 AM by James_D RSS

    Tableview / ComboBox Cell

    943794

      I am trying to figure out how to use a combo box in a cell for a table view, but I can not find any good examples.  I have data that is composed of a key and description.  I want to show the description and the have key returned as the value of the entry that the user selected.  I also will be changing the data that fills the ComboBox based on the value of another cell in the same table.  If anyone can give me a simple example that shows how that is done, I would appreciate it.

      Thank you

      Freddy

        • 1. Re: Tableview / ComboBox Cell
          James_D
          I have data that is composed of a key and description.  I want to show the description and the have key returned as the value of the entry that the user selected.

          To get this behavior for a combo box, you can do something like


          final Map<K,V> data = ... ;
          final ObservableList<K> comboBoxData = FXCollections.observableArrayList(data.keySet());
          final ComboBox<K> comboBox = new ComboBox(comboBoxData);
          comboBox.setCellFactory(new Callback<ListView<K>, ListCell<K>>() {
               @Override
               public ListCell<K> call(ListView<K> list) {
                    return new ListCell<K>() {
                         @Override
                         public void updateItem(K item, boolean empty) {
                              if (empty)  {
                                   setText(null) ;
                              } else {
                                   setText(data.get(item).toString());
                              }
                         }
                    };
               }
          });
          


          (obviously replace K and V with the type for your key and value, respectively).


          To incorporate into a table, I assume that the contents for the combo box will vary row to row in the table. This thread: List as a table cell discusses ways to manage that kind of scenario, though you will be returning a combo box configured as above instead of the VBox in that example.