JavaFX – how does cellvaluefactory() and cellfactory() work? What’s the difference between them?
I want to know about these methods cellvaluefactory(); And cellfactory();
When will it be called?
What is their purpose?
How do they work?
Solution
Both factories are used by tableview (or, more accurately, its skin)
When tableview determines that it needs a cell to display its data, use cellfactory to create a TableCell UI element The data displayed in the cell (= item) may change (scroll, modify tableview item) The items displayed in TableCell are determined by tableview using cellvaluefactory Tableview determines which items need to be displayed in a given cell and uses cellvaluefactory to obtain the observablevalue containing the instance
Cellfactory can be used to customize the appearance of cells in the table, but usually this is not necessary, because the default cellfactory only uses the toString method of its item to get the string to be displayed
Cellvaluefactory is used to "select" some items of tableview that should be displayed in the given column
In this example, you can also see the modification of tableview item properties
commit.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { Person p = tableView.getSelectionModel().getSelectedItem(); p.setName(name.getText()); Double value = sizeFormatter.getValue(); p.setSize(value == null ? -1d : value); } });
When you click a button, the following occurs:
>Modify the properties to notify the listener that tableview has been added to the observablevalues returned by cellvaluefactories. > Tableview updates the tablecells item in the row containing the modified item; Call the updateitem method with the new value In this case, the default factory is used, so updateitem just sets item Tostring() is set to the new text