Ziva
Ziva

Reputation: 21

How to make combobox editable in a tablecell in javafx?

I have set a Combobox in a tablecell using ComboBoxTableCell and now i want this combobox editable so that a user can edit it accordingly. I have made the editable property of combobox true but got no success. Below is the code.

            ComboBoxTableCell combo = new ComboBoxTableCell();                
           tc_target.setCellFactory(combo.forTableColumn(new 
            DefaultStringConverter(), trans));
            tc_target.setOnEditCommit(new 
          EventHandler<TableColumn.CellEditEvent<File, String>>() {

                @Override
        public void handle(TableColumn.CellEditEvent<File, String>event) {
                //work to do
           }

            });
     combo.setEditable(true);

how can i make this combobox editable ? Glad for any advice.

Upvotes: 1

Views: 1145

Answers (2)

Helg893
Helg893

Reputation: 1

you can make this combobox editable like this

tc_target.setCellFactory(param -> {
    ComboBoxTableCell<File, String> tc = new ComboBoxTableCell<>(new DefaultStringConverter(), trans);
    tc.setComboBoxEditable(true);
    return tc;
});

tc_target.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<File, String>>() {

    @Override
    public void handle(CellEditEvent<File, String> event) {
        // work to do
    
    }

}

Upvotes: 0

Jai
Jai

Reputation: 8363

The ComboBoxTableCell (and other similar classes) is not too clear in its Javadoc. The requirement to use ComboBoxTableCell correctly is:

  1. The TableColumn must be editable.
  2. The TableView that it belongs to must also be editable.

If you need to make sure other columns are not editable, then explicitly call TableColumn.setEditable(false) on those columns.

As a side note, you do not need to write setOnEditCommit(), and I am not sure if it would break the ComboBoxTableCell when you do so.

Upvotes: 1

Related Questions