0

I want to get one selected model with name, author,key_words to next window. Where it will be in tex fields. After i changing this, i want to save it in database by SQL Update command

public void displaySelected(ActionEvent mouseEvent) {

    ModelTable selection = Table_View.getSelectionModel().getSelectedItem();
    if (selection == null){
        System.out.println("not selected");
    }else {
        String SelectedAuthor = selection.getAuthor();
        String SelectedName = selection.getName();
        String SelectionWord = selection.getWords();

        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("Admin_Change.fxml"));
        try {
            loader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Change_Add_Button.getScene().getWindow().hide();
        Parent root = loader.getRoot();
        Stage stage = new Stage();
        stage.setScene(new Scene(root));
        stage.show();
        System.out.println("Change display");
    }
}

I want to set text field from selected item to new page to Controller_Admin_Change

@FXML
private TextField Change_AdminEdit_Author;

@FXML
private TextField Change_AdminEdit_Name;

@FXML
private TextField Change_AdminEdit_Word;

Maybe its common mistake of OOP, but idk how to find a solution Thanks for advice

Selected item what i what to insert From this

to this fields To this

  • Have a look at this also. https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx – SedJ601 May 14 '22 at 05:18

1 Answers1

3

You can get a reference to the controller via method getController in class javafx.fxml.FXMLLoader. Then you can invoke methods of the controller class.

Add a method for setting the text of Change_AdminEdit_Author, in the controller class:

public void setAuthor(String text) {
    Change_AdminEdit_Author.setText(text);
}

Call the method from displaySelected method:
(Note I assume that your controller class is named Controller_Admin_Change)

Controller_Admin_Change controller = loader.getController();
controller.setAuthor(SelectedAuthor);

It is recommended to adhere to Java naming conventions. The below code is the same as the above code but the names are according to the conventions.

public void setAuthor(String text) {
    changeAdminEditAuthor.setText(text);
}

String selectedAuthor = selection.getAuthor();
ControllerAdminChange controller = loader.getController();
controller.setAuthor(selectedAuthor);
Abra
  • 19,142
  • 7
  • 29
  • 41