I want to implement the edit button, the data from the TableView ( getSelectedItem ) should be passed to another form in TextField . When starting, I get:

 at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097) 

Controller:

 public class ControllerMain implements Initializable { static ObservableList<UserData> data; @FXML private Button btnAdd; @FXML public TableView<UserData> table; @FXML private TableColumn<UserData, String> column1; @FXML private TableColumn<UserData, Integer> column2; @FXML private TableColumn<UserData, String> column3; @FXML private TableColumn<UserData, String> column4; @Override public void initialize(URL location, ResourceBundle resources) { try (Connection con = new DBConnect().getConnected(); ResultSet rs = con.createStatement().executeQuery("SELECT * FROM job.job")) { data = FXCollections.observableArrayList(); while (rs.next()) { data.add(new UserData(rs.getString("company_name"), rs.getInt("phone"), rs.getString("address"), rs.getString("other"))); } String companyName = "companyName"; column1.setCellValueFactory(new PropertyValueFactory<UserData, String>(companyName)); String phone = "phone"; column2.setCellValueFactory(new PropertyValueFactory<UserData, Integer>(phone)); String address = "address"; column3.setCellValueFactory(new PropertyValueFactory<UserData, String>(address)); String other = "other"; column4.setCellValueFactory(new PropertyValueFactory<UserData, String>(other)); table.setId("cell-style"); table.setItems(null); table.setItems(data); } catch (Exception e) { e.printStackTrace(); System.out.println("Error on Building Data"); } } @FXML private void openUpdateForm() { try { Parent root = FXMLLoader.load(getClass().getResource("/fxml/updateForm.fxml")); Stage stage = new Stage(); stage.initModality(Modality.APPLICATION_MODAL); stage.initStyle(StageStyle.UNIFIED); stage.setTitle("Редактирование"); stage.setScene(new Scene(root)); stage.show(); } catch (Exception e) { System.err.println("Ошибка открытия формы редактивания: " + e.getMessage()); e.printStackTrace(); } } 

And another edit window controller:

  public class ControllerUpdateData implements Initializable { @FXML private TextField txt2; @FXML private TextField txt3; @FXML private TextField txt4; @FXML public TextField txt1; @Override public void initialize(URL location, ResourceBundle resources) { txt1.setText(new ControllerMain().table.getSelectionModel().getSelectedItem().companyNameProperty().getValue()); } } 

    1 answer 1

    FXMLLoader has a getController method:

     ... private void openUpdateForm(){ try { FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/updateForm.fxml")); Parent root = fxmlLoader.load(); // получаем экземпляр контроллера ControllerUpdateData controller = fxmlLoader.getController(); // передаем ему выделенный элемент controller.setUserData(table.getSelectionModel().getSelectedItem()); Stage stage = new Stage(); stage.initModality(Modality.APPLICATION_MODAL); stage.initStyle(StageStyle.UNIFIED); stage.setTitle("Редактирование"); stage.setScene(new Scene(root)); stage.show(); } catch (Exception e) { System.err.println("Ошибка открытия формы редактивания: " + e.getMessage()); e.printStackTrace(); } } class ControllerUpdateData { ... public void setUserData(UserData selectedItem) { // обновляем UI txt1.setText(selectedItem.companyNameProperty().getValue()); // или txt1.textProperty().bindBidirectional(selectedItem.companyNameProperty()); } ... }