5

I followed this Oracle tutorial to create a TableView in FXML. However there is no info on how to make a cell editable. I have tried other tutorials suggesting to add something like firstNameCol.setOnEditCommit( new EventHandler ... into the controller code, but it didn't work. No matter how I click the mouse or punch the keyboard, the table cell does not change into the "editable textbox", as seen in here:

enter image description here

Here is my FXML definition of the table:

<TableView fx:id="tvAlgorithmSteps" editable="true" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="200.0" VBox.vgrow="ALWAYS">
    <columns>
        <TableColumn editable="true" prefWidth="150.0" sortable="false" text="Step">
            <cellValueFactory>
                <PropertyValueFactory property="stepName" />
            </cellValueFactory>
        </TableColumn>
        ...

The autocomplete allows me to set onEditCommit="" on the TableColumn in the FXML, but I have no idea what to put in there.

How should I edit the FXML to allow edits on the cells?

Leprechaun
  • 852
  • 6
  • 25

1 Answers1

7

You need to use a cellFactory creating editable cells, e.g. TextFieldTableCells. If the PropertyValueFactory returns a writable property, onEditCommit shouldn't be necessary

<TableColumn editable="true" prefWidth="150.0" sortable="false" text="Step">
    <cellValueFactory>
        <PropertyValueFactory property="stepName" />
    </cellValueFactory>
    <cellFactory>
        <TextFieldTableCell fx:factory="forTableColumn" />
    </cellFactory>
</TableColumn>

You may need to add a processing instruction to import the TextFieldTableCell class to the beginning of the file (but after <?xml ...):

<?import javafx.scene.control.cell.TextFieldTableCell?>
fabian
  • 80,457
  • 12
  • 86
  • 114
  • Is this supposed to be enough to change values in model objects (through SimpleStringProperties)? Or am I missing something? – Leprechaun Aug 12 '16 at 11:45
  • @Leprechaun as long as there is a property getter returning the property (e.g. `StringProperty stepNameProperty()`), it should work, see http://stackoverflow.com/documentation/javafx/2229/tableview/7289/propertyvaluefactory#t=201608121148083945414 – fabian Aug 12 '16 at 11:48
  • I am having issues with this implementation not binding to stepName when you make a change. it has no issues retrieving the value though. – Travis Tubbs Sep 18 '18 at 19:34