Javafx Tableview MapValueFactory. How to edit a cell and save the new value

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Javafx Tableview MapValueFactory. How to edit a cell and save the new value



I'm using this example http://docs.oracle.com/javafx/2/ui_controls/table-view.htm where tableview is based on MapValueFactory.



Now I have the situation when edited values are not saved. My actions:



Press "Enter" -> type new value -> press "Enter" again -> Result: New value is shown in the cell.



But when I press "Enter" in this cell again the old value is shown instead of New value.



Please help me to understand how can I save changes after editing the cell?


Example 12-12 Adding Map Data to the Table

import java.util.HashMap;
import java.util.Map;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.MapValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;

public class TableViewSample extends Application

public static final String Column1MapKey = "A";
public static final String Column2MapKey = "B";

public static void main(String args)
launch(args);


@Override
public void start(Stage stage)
Scene scene = new Scene(new Group());
stage.setTitle("Table View Sample");
stage.setWidth(300);
stage.setHeight(500);

final Label label = new Label("Student IDs");
label.setFont(new Font("Arial", 20));

TableColumn<Map, String> firstDataColumn = new TableColumn<>("Class A");
TableColumn<Map, String> secondDataColumn = new TableColumn<>("Class B");

firstDataColumn.setCellValueFactory(new MapValueFactory(Column1MapKey));
firstDataColumn.setMinWidth(130);
secondDataColumn.setCellValueFactory(new MapValueFactory(Column2MapKey));
secondDataColumn.setMinWidth(130);

TableView table_view = new TableView<>(generateDataInMap());

table_view.setEditable(true);
table_view.getSelectionModel().setCellSelectionEnabled(true);
table_view.getColumns().setAll(firstDataColumn, secondDataColumn);
Callback<TableColumn<Map, String>, TableCell<Map, String>>
cellFactoryForMap = new Callback<TableColumn<Map, String>,
TableCell<Map, String>>()
@Override
public TableCell call(TableColumn p)
return new TextFieldTableCell(new StringConverter()
@Override
public String toString(Object t)
return t.toString();

@Override
public Object fromString(String string)
return string;

);

;
firstDataColumn.setCellFactory(cellFactoryForMap);
secondDataColumn.setCellFactory(cellFactoryForMap);

final VBox vbox = new VBox();

vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table_view);

((Group) scene.getRoot()).getChildren().addAll(vbox);

stage.setScene(scene);

stage.show();


private ObservableList<Map> generateDataInMap()
int max = 10;
ObservableList<Map> allData = FXCollections.observableArrayList();
for (int i = 1; i < max; i++)
Map<String, String> dataRow = new HashMap<>();

String value1 = "A" + i;
String value2 = "B" + i;

dataRow.put(Column1MapKey, value1);
dataRow.put(Column2MapKey, value2);

allData.add(dataRow);

return allData;





1 Answer
1



You need to define the cellEditEvent with setOnEditCommit:


firstDataColumn.setOnEditCommit((CellEditEvent<Map<String, String>, String> t) ->
((Map<String, String>) t.getTableView().getItems().get(t.getTablePosition().getRow())).put(Column1MapKey, t.getNewValue());
);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard