tableviewjavafx-8

TableView, setting editable cells


I try to make table cells editable. I managed to do this with two columns that have String values in it, but I can't make this with columns that represent Integer values.

Places with X is where compiler get the error:

The method setCellFactory(Callback<TableColumn<DataModel,Integer>,TableCell<DataModel,Integer>>) in the type TableColumn<DataModel,Integer> is not applicable for the arguments (Callback<TableColumn<DataModel,String>,TableCell<DataModel,String>>)

and places with XX is where compiler get the error:

The method setOnEditCommit(EventHandler<TableColumn.CellEditEvent<DataModel,Integer>>) in the type TableColumn<DataModel,Integer> is not applicable for the arguments ((CellEditEvent<DataModel, Integer> event) -> {})

Here's the code:

public void initialize(URL location, ResourceBundle resources) 
{
    //Tworzymy sobie kolumny, które będą odpowiadać oraz przyjmować konretne dane
    TableColumn<DataModel, String> nameColumn = new TableColumn<DataModel, String>("Name");
    nameColumn.setMinWidth(100);
    TableColumn<DataModel, String> surnameColumn = new TableColumn<DataModel, String>("Surname");
    surnameColumn.setMinWidth(100);
    TableColumn<DataModel, Integer> ageColumn = new TableColumn<DataModel, Integer>("Age");
    ageColumn.setMinWidth(100);
    TableColumn<DataModel, Integer> telNumberColumn = new TableColumn<DataModel, Integer>("Tel. Number");
    telNumberColumn.setMinWidth(100);

    //dodajemy kolumny do okna
    tableView.getColumns().addAll(nameColumn,surnameColumn,ageColumn,telNumberColumn);
    
    //podajemy nazwy zmiennych, których wartości mają się wyświetlać w poszczególnych kolumnach
    nameColumn.setCellValueFactory(new PropertyValueFactory<>("sName"));
    surnameColumn.setCellValueFactory(new PropertyValueFactory<>("sSurname"));
    ageColumn.setCellValueFactory(new PropertyValueFactory<>("iAge"));
    telNumberColumn.setCellValueFactory(new PropertyValueFactory<>("iPhoneNumber"));
    
    //Sprawiamy że poszczególne kolumny stają się edytowalne
    nameColumn.setCellFactory(TextFieldTableCell.<DataModel>forTableColumn());
    nameColumn.setOnEditCommit((CellEditEvent<DataModel, String> event) -> {
        ((DataModel) event.getTableView().getItems(). get(event.getTablePosition().getRow())).  setsName(event.getNewValue());
    });
    surnameColumn.setCellFactory(TextFieldTableCell.<DataModel>forTableColumn());
    surnameColumn.setOnEditCommit((CellEditEvent<DataModel, String> event) -> {
        ((DataModel) event.getTableView().getItems(). get(event.getTablePosition().getRow())).  setsSurname(event.getNewValue());
    });
X   ageColumn.setCellFactory(TextFieldTableCell.<DataModel>forTableColumn());
XX  ageColumn.setOnEditCommit((CellEditEvent<DataModel, Integer> event) -> {
    //    ((DataModel) event.getTableView().getItems(). get(event.getTablePosition().getRow())).  setiAge(Integer.valueOf(event.getNewValue()));

    });
X   telNumberColumn.setCellFactory(TextFieldTableCell.<DataModel>forTableColumn());
XX  telNumberColumn.setOnEditCommit((CellEditEvent<DataModel, Integer> event) -> {
 //         ((DataModel) event.getTableView().getItems(). get(event.getTablePosition().getRow())).  setiPhoneNumber(Integer.valueOf(event.getNewValue()));

    });
    
    tableView.setPlaceholder(new Label("Pust tabelka!"));//jaki element dodać jeśli tabelka nie jest wyświetlona
    tableView.setEditable(true);
    
    tableView.setItems(dataList); //wczytujemy dane do przygotowanej tabelki
    
    buttAdd.setOnAction((ActionEvent e) -> {
        buttAddAction(e);
    });
}

I'm taking Oracle TableView tutorial, and it's quite difficult. Help.


Solution

  • The issue is that TextFieldTableCell.forTableColumn() is typed to a String value. See the default implementation:

    public static <S> Callback<TableColumn<S,String>, TableCell<S,String>> forTableColumn() {
        return forTableColumn(new DefaultStringConverter());
    }
    

    What you need is the TextFieldTableCell with an IntegerStringConverter, for example:

    ageColumn.setCellFactory(TextFieldTableCell.<DataModel, Integer>forTableColumn(new IntegerStringConverter()));