0

I use tableview for my project. As I have long text to put into the cells, I need to wrap it but I also need to edit cells.

So, I have this code.

tcHelp.setCellValueFactory(new PropertyValueFactory<Help, String>("Description"));
tcHelp.setCellFactory(TextFieldTableCell.forTableColumn());

tcHelp.setCellFactory(new Callback<TableColumn<Help, String>, TableCell<Help, String>>() {

@Override
public TableCell<Help, String> call(TableColumn<Help, String> param) {
          final TableCell<Help, String> cell = new TableCell<Help, String>() {

        private Text text;
        @Override
        public void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);
            if (!isEmpty()) {
                Help rowItem = getTableRow().getItem();
                    text = new Text(rowItem.getDescription());
                text.setWrappingWidth(440);
                setGraphic(text);
                    }
            }
        };
                
    //Don't work!!
    cell.setEditable(true);

                
    return cell;
    }
});

I have a problem to edit the cells. What's wrong with cell.setEditable(true) ?

edit : According to trashgod advice, i wrote that but I can't save the value edited. What's wrong with this code ?

    tcHelp.setCellFactory(new Callback<TableColumn<Help, String>, TableCell<Help, String>>() {
        @Override
        public TableCell<Help, String> call(TableColumn<Help, String> param) {
            final TableCell<Help, String> cell = new TableCell<Help, String>() {

                private TextArea textArea;

                @Override
                public void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if (!isEmpty()) {
                        Help rowItem = getTableRow().getItem();
                        String text= rowItem.getDescription();
                        Text t = new Text(text);
                        textArea = new TextArea(text);
                        textArea.setWrapText(true);
                        double height = t.getLayoutBounds().getHeight();
                        textArea.setPrefHeight(height+10);
                        setGraphic(textArea);
                    }
                }
            };
            cell.setEditable(true);

            return cell;
        }
    });

setOnEditCommit is never called with textarea in my example !!

    tcHelp.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<Help, String>>() {

        @Override
        public void handle(TableColumn.CellEditEvent<Help, String> t) {
            // TODO Auto-generated method stub

            ((Help) t.getTableView().getItems().get(t.getTablePosition().getRow())).setDescription(t.getNewValue());
            

    });
  • 1
    `new PropertyValueFactory("Description")` is wrong. "Description" is not a valid bean property name. See https://stackoverflow.com/questions/65785787/javafx-tableview-not-showing-data-something-wrong-with-getter-setters/65786210#65786210. – VGR May 13 '23 at 23:19
  • 1
    Have a look at the [implementation of `TextFieldTableCell`](https://github.com/openjdk/jfx/blob/70953643a9dc05c76924fc4c602ee39038d71295/modules/javafx.controls/src/main/java/javafx/scene/control/cell/TextFieldTableCell.java), which relies heavily on a class called [`CellUtils`](https://github.com/openjdk/jfx/blob/70953643a9dc05c76924fc4c602ee39038d71295/modules/javafx.controls/src/main/java/javafx/scene/control/cell/CellUtils.java), for the basic pattern to make this work. – James_D May 14 '23 at 01:00
  • 1
    In order for a cell to be editable, the `TableView.editable` property, the `TableColumn.editable` property, and the `TableCell.editable` property **all** need to be true. The latter two are true by default, if I'm not mistaken. So, you need to call `theTable.setEditable(true)`. Note that calling `setEditable(true)` does not put the cell into editing mode. It only makes it _possible_ for the cell to enter editing mode. Though if the cell is editable, then doing something like double-clicking the cell should put it in editing mode. – Slaw May 14 '23 at 01:00
  • 1
    A [*very* old tutorial](https://docs.oracle.com/javafx/2/ui_controls/table-view.htm#CJAGAAEE) (which basically includes some code from a version of JavaFX that predates the `TextFieldTableCell` includes an `EditingCell` implementation that might also be helpful, and perhaps easy to follow that the code I linked above. – James_D May 14 '23 at 01:04
  • Typically, an editable cell has a different UI when in editing mode compared to read-only mode. If one of the built-in cells in the `javafx.scene.control.cell` package fits your needs, then I strongly recommend you use one of them. But if you need a custom UI for editing the cell's value, then you'll need to override certain methods in the cell. Look at the implementations in `javafx.scene.control.cell` for inspiration (or try to find a tutorial). Though creating a custom editable cell is not necessarily trivial. It may be easier for you to simply display another view for editing in that case. – Slaw May 14 '23 at 01:04

0 Answers0