0

I'm trying to set a text value for a JFXTextField in an FXML by clicking a row in another FXML Tableview, however, this is what I've tried

Controller 1

public void setTextField(String tes){
   this.txtKelas.setText(tes);
}

Controller 2

@FXML
    void clickItem(MouseEvent event) {
        if (event.getClickCount() == 1 || event.getClickCount() == 2) {
            if (tableView.getSelectionModel().getSelectedItem() == null) {
                System.out.println("Null");
            } else if(tableView.getSelectionModel().getSelectedItem() != null){
                int index = tableView.getSelectionModel().getSelectedIndex();
                FXMLLoader load = new FXMLLoader();
                Kereta_Controller controller;
                load.setLocation(getClass().getResource("/com/kereta_api/views/Dashboard/Manager/Menu/Kereta_Api.fxml"));
                try {
                    load.load();
                    controller = load.getController();
                    controller.setTextField(tableView.getColumns().get(0).getCellObservableValue(index).getValue().toString());
                } catch (IOException ex) {
                    Logger.getLogger(Pilih_Kelas_Controller.class.getName()).log(Level.SEVERE, null, ex);
                }
                Stage stage = (Stage) tableView.getScene().getWindow();
                stage.close();
            }
        }
    }

After I run the program the textfield didn't update I wonder why? is there any possible solutions for this problem? thank you!

AmarWibi99
  • 11
  • 5
  • The text field you’re updating isn’t displayed anywhere (you just call `loader.load()` and discard the result), so you won’t see any effect. Consider using a MVC approach instead of directly linking controllers to each other. https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx – James_D Jun 27 '20 at 12:05
  • @James_D I guess there still many things that I need to be fixed with the program, considering it still really messy, thanks for the suggestion, gonna check it out – AmarWibi99 Jun 30 '20 at 08:57

1 Answers1

-1

You can solve this problem by using a static variable, try this:

Controller 1:

public static JFXTextField txtKelasStatic;

@Override
public void initialize(URL location, ResourceBundle resources) {
    txtKelasStatic = txtKelas;
}

PS: You need to implement the Initializable interface in the Controller 1.

Controller 2:

@FXML
void clickItem(MouseEvent event) {
    if (event.getClickCount() == 1 || event.getClickCount() == 2) {
        if (tableView.getSelectionModel().getSelectedItem() == null) {
            System.out.println("Null");
        } else if(tableView.getSelectionModel().getSelectedItem() != null){
            int index = tableView.getSelectionModel().getSelectedIndex();

            Kereta_Controller.txtKelasStatic.setText(tableView.getColumns().get(0).getCellObservableValue(index).getValue().toString());

            Stage stage = (Stage) tableView.getScene().getWindow();
            stage.close();
        }
    }
}
Houari Zegai
  • 98
  • 1
  • 10
  • 1
    while possible, (mis-) using static scope for parameter passing is the worst of options – kleopatra Jun 29 '20 at 08:18
  • @kleopatra can I know the reason why is it not a good option? – AmarWibi99 Jun 30 '20 at 08:58
  • @AmarWibi99 1. `static` scope is just wrong here: the text field is a property of the specific copy of the UI that was loaded, not a property of the class. If you later decided you wanted to load the FXML and display two or more copies in your application, this would break completely. Since this appears to be a "detail pane" for a table item, it's quite feasible you might later decide to show multiple detail panes for multiple selected items. – James_D Jun 30 '20 at 11:32
  • @AmarWibi99 2. It breaks encapsulation. The UI controls should be considered an implementation detail of the FXML-controller pair and should not be made public. If you later decided, e.g., you wanted to replace the text field with a combo box (or just stop using JFoenix), you'd have to trawl through all your code to see what needed changing. UI controls should *never* be publicly exposed (either directly through the field or via a method). – James_D Jun 30 '20 at 11:33
  • Although the solution I suggested works, but I agree with you it may cause other problems if you want to create a multiple instance of UI. – Houari Zegai Jun 30 '20 at 18:22