3 Replies Latest reply: Feb 26, 2013 3:18 AM by 993317 RSS

    Binding Map with TableView

    993317
      Hi,
      I have a Map<String,String> which i want to display in a TableView with 2 colums(Key column and Value column). I am able to display the data by creating an ObservableList of objects having the key and value attributes but i want to bind the Map with the TableView. I have not come across anything related to this yet. Please help,

      Thanks,
      Avinash
        • 1. Re: Binding Map with TableView
          jsmith
          See the "Adding Maps of Data to the Table" example from the JavaFX documentation:
          http://docs.oracle.com/javafx/2/ui_controls/table-view.htm

          The key is to make use of the MapValueFactory:
          http://docs.oracle.com/javafx/2/api/javafx/scene/control/cell/MapValueFactory.html
          • 2. Re: Binding Map with TableView
            993317
            Thanks John but the solution you gave is also doing a similar thing as i was doing. It is using an ObservableList<Map>.
            I guess there is no direct way of populating the Table with the entries from a single Map instance. And I also need to bind the Map data so that any change i make in the Table from UI gets reflected in the Map data. I will post whatever i come up with.

            Regards.
            • 3. Re: Binding Map with TableView
              993317
              This is working for me.
              public class Table extends TableView<Map.Entry> {
              
                  public Table() {
                      super();
                      setEditable(true);
                      setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
                      
                      buildColumns();
                      populate();
                  }
              
                  private void buildColumns() {
                      TableColumn<Map.Entry, String> nCol = new TableColumn<Map.Entry, String>("Name");
                      nCol.setEditable(true);
                      nCol.setCellFactory(new Callback<TableColumn<Map.Entry, String>, TableCell<Map.Entry, String>>() {
              
                          @Override
                          public TableCell<Entry, String> call(TableColumn<Entry, String> param) {
                              return new CustomTableCell();
                          }
                      });
                      nCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Entry, String>, ObservableValue<String>>() {
                          @Override
                          public ObservableValue<String> call(CellDataFeatures<Entry, String> param) {
                              String key = (String) param.getValue().getKey();
                              return new SimpleObjectProperty<String>(key);
                          }
                      });
                      
                      TableColumn<Map.Entry, String> vCol = new TableColumn<Entry, String>("Value");
                      vCol.setEditable(true);
                      vCol.setCellFactory(new Callback<TableColumn<Entry, String>, TableCell<Entry, String>>() {
                          @Override
                          public TableCell<Entry, String> call(TableColumn<Entry, String> param) {
                              return new CustomTableCell();
                          }
                      });
                      
                      vCol.setCellValueFactory(new Callback<CellDataFeatures<Entry, String>, ObservableValue<String>>() {
                          @Override
                          public ObservableValue<String> call(CellDataFeatures<Entry, String> param) {
                              String value = (String) param.getValue().getValue();
                              return new SimpleStringProperty(value);
                          }
                      });
                      
                      getColumns().setAll(nCol, vCol);
                  }
              
                  private void populate() {
                      Map<String, String> data = new HashMap<String, String>();
                      data.put("a", "aa");
                      data.put("b", "bb");
                      data.put("c", "cc");
                      data.put("d", "dd");
                      
                      Set<Entry<String, String>> dataSet = data.entrySet();     
                      ObservableList tableData = FXCollections.observableArrayList(dataSet); 
                      setItems(tableData);
                  }
                  
              }
              The binding is still pending.