2

I am Creating a tableview with 2 columns and each populated cell of this table is a ComboBox. This is how I am implementing this

    private void loadTable() {
        vBox.getChildren().clear();
        this.tableView = new TableView<>();
        tableView.setEditable(true);

        TableColumn<CustomClass, Products> prodCol = createComboBoxColumn("products", "Product", Products.class,
                productObList);
        prodCol.setMinWidth(300);
        prodCol.setOnEditCommit(e -> {
            if (!e.getTableView().getItems().isEmpty()) {
                System.out.println("e.getNewValue().getId(): " + e.getNewValue().getId());
                Products product = productsModel.getProductsById(e.getNewValue().getId());
                e.getTableView().getItems().get(e.getTablePosition().getRow()).setProducts(product);
            }
            tableView.refresh();
        });
        tableView.getColumns().add(prodCol);

        TableColumn<CustomClass, Brand> brandCol = createComboBoxColumn("brand", "Brand", Brand.class,
                brandObList);
        brandCol.setMinWidth(300);
        brandCol.setOnEditCommit(e -> {
            if (!e.getTableView().getItems().isEmpty()) {
                System.out.println("e.getNewValue().getId(): " + e.getNewValue().getId());
                Brand brand = brandModel.getBrandById(e.getNewValue().getId());
                e.getTableView().getItems().get(e.getTablePosition().getRow()).setBrand(brand);
            }
            tableView.refresh();
        });
        tableView.getColumns().add(brandCol);
//      tableView.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {

//          @Override
//          public void handle(MouseEvent event) {
//              if (event.getClickCount() == 2) {
//                  System.out.println("on Click");
//                  if (event.getTarget() instanceof ComboBox) {
//                      System.out.println(((ComboBox) event.getTarget()).getSelectionModel().getSelectedItem());
//                  }
//              }
//          }

//      });

        tableView.getItems().addAll(brandManifestCustomObList);
        vBox.getChildren().addAll(tableView);

    }

and the createComboboxColumn Method as

public static <T> TableColumn<CustomClass, T> createComboBoxColumn(String name, String columHeading,
            Class<T> type, ObservableList list) {
        TableColumn<CustomClass, T> column = new TableColumn<>(columHeading);
        column.setCellFactory(ComboBoxTableCell.forTableColumn(list));
        column.setResizable(true);
        column.setMaxWidth(100);
        column.setEditable(true);
        column.setCellValueFactory(new PropertyValueFactory<>(name));
        return column;
    }

What I am not able to achieve is to detect double click(or even single click) on the tablecell combo box. Once I select some other row and come back to this tablecell and doubleclick, only then I am able to detect the double click on the cell.

The obvious reason is once I click on a cell it becomes a Combobox and I have not written code to detect it, because I don't understand how to in this case. Also, if possible, I need to detect the column index of the cell in the tableview. Following questions do not answer what I am looking for:

1) JavaFx: ComboBox Table Cell double click

2) Detect mouse click on SELECTION Editable ComboBox JavaFX

3) Select JavaFX Editable Combobox text on click

Nitin Nanda
  • 805
  • 2
  • 11
  • 27

1 Answers1

1

Probably you can use

tableView.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                if (event.getClickCount() == 2) {
                    System.out.println("on Click");

                    if (event.getTarget() instanceof ComboBox) {
                        System.out.println(((ComboBox) event.getTarget()).getSelectionModel().getSelectedItem());
                    }
                    if (event.getTarget() instanceof ComboBoxTableCell<?,?>) {
                        System.out.println(((ComboBoxTableCell) event.getTarget()).getItem().toString());
                    }
                }
            }



        });

because the table cell has now changed to a ComboBoxTableCell. To get the column of the tableView I got some idea from here given by James_D. You can use

TablePosition pos = tableView.getSelectionModel().getSelectedCells().get(0);
        int row = pos.getRow();
        BrandManifestCustom bmc = tableView.getItems().get(row);
        TableColumn col = pos.getTableColumn();
        if (col.getCellObservableValue(bmc).getValue() instanceof Products) {
            System.out.println("hey Products");
        }
        if (col.getCellObservableValue(bmc).getValue() instanceof Brand) {
            System.out.println("hey Brand");
        }