How can I set value at selected row in TableColumn JavaFX. Early I did it in Swing with table.setValueAt, but now I do it in JavaFX and have some problem with doing it.
I'm doing Database App. There are a three fields for ID, first name and last name. And a three buttons: Add, Update, Delete.
I need to replace old values to new values after click Update button. I've done Select method, now I need to make Update method. Here's my full code.
Controller class
package com.cascado.application.application;
import static com.cascado.application.common.Constants.REGEX;
import com.cascado.application.common.Constants;
import com.cascado.application.common.User;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import java.net.URL;
import javafx.scene.input.MouseEvent;
import java.util.ResourceBundle;
public class Controller implements Initializable {
    @FXML
    private TableView<User> table;
    @FXML
    private TableColumn<User, String> idColumn;
    @FXML
    private TableColumn<User, String> firstNameColumn;
    @FXML
    private TableColumn<User, String> lastNameColumn;
    @FXML
    private TextField firstNameField;
    @FXML
    private TextField lastNameField;
    @FXML
    private TextField idField;
    private ObservableList<User> users;
    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        idColumn.setCellValueFactory(new PropertyValueFactory<User, String>("ID"));
        firstNameColumn.setCellValueFactory(new PropertyValueFactory<User, String>("firstName"));
        lastNameColumn.setCellValueFactory(new PropertyValueFactory<User, String>("lastName"));
        users = table.getItems();
        table.setEditable(true);
        selectRow();
    }
    @FXML
    private void addButton(ActionEvent actionEvent) {
        users.add(newUser(Constants.ID, Constants.FIRST_NAME, Constants.LAST_NAME));
        table.setItems(users);
        clearFields();
    }
    private void selectRow(){
        table.setOnMouseClicked((MouseEvent event) -> {
            getTextFromSelectedRow();
        });
    }
    private void getTextFromSelectedRow(){
        User selectedUser = table.getSelectionModel().getSelectedItem();
        idField.setText(selectedUser.getID());
        firstNameField.setText(selectedUser.getFirstName());
        lastNameField.setText(selectedUser.getLastName());
    }
    @FXML
    private void updateButton(ActionEvent actionEvent) {
        if (table.getSelectionModel().getSelectedIndex() != -1) {
            setTextToSelectedRow();
        } else {
            Alert alert = new Alert(Alert.AlertType.CONFIRMATION,
                    "Please select the row",
                    ButtonType.OK);
            alert.showAndWait();
        }
    }
    private void setTextToSelectedRow(){
//        int selectedUser = table.getSelectionModel().getSelectedIndex();
        idColumn.setCellValueFactory();
        firstNameColumn.setText(firstNameField.getText());
        lastNameColumn.setText(lastNameField.getText());
    }
    @FXML
    private void deleteButton(ActionEvent actionEvent) {
    }
    private String[] fieldsTextArray(){
        return (idField.getText() + REGEX + firstNameField.getText() + REGEX + lastNameField.getText()).split(REGEX);
    }
    private void clearFields(){
        firstNameField.clear();
        lastNameField.clear();
        idField.clear();
    }
    private User newUser(int id, int firstName, int lastName){
        // values in textfields message array
        return new User(fieldsTextArray()[id], fieldsTextArray()[firstName], fieldsTextArray()[lastName]);
    }
}
Application class
package com.cascado.application.application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Application extends javafx.application.Application {
    public static void run(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(this.getClass().getResource("/resources.fxml"));
        Parent parent = loader.load();
        Scene scene = new Scene(parent);
        primaryStage.setScene(scene);
        primaryStage.setResizable(false);
        primaryStage.setTitle("Database");
        primaryStage.show();
    }
}
Constants class
package com.cascado.application.common;
public class Constants {
    public static final String REGEX = "@#!#@";
    public static final int ID = 0;
    public static final int FIRST_NAME = 1;
    public static final int LAST_NAME = 2;
}
User class
package com.cascado.application.common;
public class User {
    private String firstName;
    private String lastName;
    private String ID;
    public User(String ID, String firstName, String lastName){
        this.ID = ID;
        this.firstName = firstName;
        this.lastName = lastName;
    }
    public String getFirstName() {
        return firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public String getID() {
        return ID;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public void setID(String ID) {
        this.ID = ID;
    }
}
