1 Reply Latest reply: Feb 14, 2013 8:57 AM by James_D RSS

    Click handler on ComboxBox inside TableView


      We have built a table and each row has a comboBox in it. How do I handle different selection events on the combobox and then get access to the row that it was changed on.
      levelCol.setCellFactory(new Callback<TableColumn<LoggerTableRow,String>,TableCell<LoggerTableRow,String>>(){        
                   public TableCell<LoggerTableRow, String> call(TableColumn<LoggerTableRow, String> param) {                
                        TableCell<LoggerTableRow, String> cell = new TableCell<LoggerTableRow, String>(){
                             public void updateItem(String item, boolean empty) {                        
                                  if(item != null){
                                       ComboBox<String> comboBox = new ComboBox<>();
                        return cell;
        • 1. Re: Click handler on ComboxBox inside TableView
          If your LoggerTableRow has a property to which that column is bound: i.e. if you have something like
          levelCol.setCellValueFactory(new PropertyValueFactory<LoggerTableRow, String>("level"));
          then you can just use the built in ComboBoxTableCell:
          The combo box is creates will automatically update the level property in the LoggerTableRow object. If you need to perform further actions when that changes, then you can add listeners to those properties.

          If you can't use a ComboBoxTableCell, then I think the steps to add a change listener to the selected item property of the combo box, and call commitEdit(...) when it changes. This will update the property associated to the column. If your LoggerTableRow is a POJO (no FX properties), then you may also need to override commitEdit(...) to update the associated value "by hand". Example 13-11 in the [url http://docs.oracle.com/javafx/2/ui_controls/table-view.htm#CJAGAAEE]TableView tutorial gives a fairly full example of a custom editing table cell (using a text field, but it shouldn't be too hard to generalize). Scroll down the code there to the EditingCell inner class.