How can I access fields in another controller

Hello... I have 2 files fxml... FXML file1 has Voletfractionne, on the left side of the Voletfractionne, there is a list (list of names) and on the right, I imported File2 FXML (which has a first and last name)... I want to do that when I select a line in the listView, the firstname and lastname in the FXML File2 is automatically populated. How will I be able to achieve this?

FXML file1

<? XML version = "1.0" encoding = "UTF-8"? >

<? import java.lang. *? >

<? import java.util? >

<? import javafx.scene.control. *? >

<? import javafx.scene.layout. *? >

<? import javafx.scene.paint. *? >

"" " < AnchorPane id ="AnchorPane"maxHeight =" "-infinite" maxWidth = "-infinite" minHeight = ""-infinite "minWidth ="-infinite "prefHeight = prefWidth"400.0"= '600.0' xmlns:fx = ' http://JavaFX.com/fxml ">

< children >

"< Voletfractionne dividerPositions"0.27759197324414714"= focusTraversable ="true"prefHeight = '400.0' prefWidth = '600.0" AnchorPane.bottomAnchor = "0.0" AnchorPane.leftAnchor ="0.0" AnchorPane.rightAnchor = "0.0" AnchorPane.topAnchor ="0.0" > "

elements <>

< AnchorPane minHeight = "0,0" minWidth = "0.0" prefHeight = "160.0" prefWidth "100.0" = >

< children >

"" < ListView fx:id = "myList" prefHeight = "398,0" prefWidth = "163,0" AnchorPane.bottomAnchor = "0.0" AnchorPane.leftAnchor ="0.0" AnchorPane.rightAnchor = "0.0" AnchorPane.topAnchor ="0.0" / >

< / children >

< / AnchorPane >

< AnchorPane minHeight = "0,0" minWidth = "0.0" prefHeight = "160.0" prefWidth "100.0" = >

< children >

"" < fx: include source = "moreInfo.fxml" prefHeight = "398,0" prefWidth = "429.0" AnchorPane.bottomAnchor ="0.0" AnchorPane.leftAnchor = "0.0" AnchorPane.rightAnchor ="0.0" AnchorPane.topAnchor = "0.0" / > "

< / children >

< / AnchorPane >

< / object >

< / Voletfractionne >

< / children >

< / AnchorPane >

FXML File2

<? XML version = "1.0" encoding = "UTF-8"? >

<? import java.lang. *? >

<? import java.util? >

<? import javafx.collections. *? >

<? import javafx.scene.control. *? >

<? import javafx.scene.layout. *? >

<? import javafx.scene.paint. *? >

"" " < AnchorPane id ="AnchorPane"maxHeight =" "-infinite" maxWidth = "-infinite" minHeight = ""-infinite "minWidth ="-infinite "prefHeight = prefWidth"400.0"= '600.0' xmlns:fx = ' http://JavaFX.com/fxml ">

< children >

< layoutX label = "78.0" on = "47.0" text = "FirstName" / >

< layoutX label = "78.0" on = "86,0" text = "Lastname" / >

< fx:id = "first name" = "135.0" layoutX TextField on = "47.0" prefWidth = "200,0" / >

< fx:id = "lastname" layoutX = '135.0' TextField on = '86,0' prefWidth = "200,0" / >

< / children >

< / AnchorPane >

See nested controllers in the introduction to FXML.

In short, you can set a fx:id on the in your first file FXML:


and then, in the controller associated with your first file FXML set a reference to the controller associated with your second FXML file:

public class MainController {
     @FXML
     private DetailsController detailsController ;
     @FXML
     private ListView myList ;
     //...
     public void initialize() {
          // ...
          myList.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
               @Override
               public void changed(Observable observable, Person oldValue, Person newValue) {
                    detailsController.setPerson(newValue);
               }
          });
     }
     // ...
}
public class DetailsController {
     @FXML
     private TextField firstname ;
     @FXML
     private TextField lastname ;
     // Here's the most naive way; maybe you really want an ObjectProperty and some bindings, etc...
     public void setPerson(Person person) {
          if (person != null) {
               firstname.setText(person.getFirstName());
               lastname.setText(person.getLastName());
          } else {
               firstname.setText("");
               lastname.setText("");
          }
     }
}

The requirement is that if the fx:id of your included resource is "xxx", the name of the variable for the corresponding controller is xxxController (for details-> detailsController).

Tags: Java

Similar Questions

Maybe you are looking for

  • DO NOT SEE JPG IN A THREAD MUST DIRECTLY CLICK ON A FILE IN THE LIST

    REF.: http://www.messerforum.net/showthread.php?129265-Peter-Herbst-Vollintegral-amp-Fritz-Sch%FCrz-Messerscheide just a list of jpg files attached to the place to see them directly. Click on each Rible on to see.

  • What CD/DVD drive is compatible with Satellite 1400-103

    Hello I've been looking for A replacement Dvd Rom for my Toshiba Satellite 1400-103 and I can't find one.Because she was so old so I was wondering if there is some other Dvd Compatible disks that I can buy for my laptop? Thank you

  • Need good drivers for Satellite C660D-1GJ

    Hello! I have this laptop Satellite C660D-1GJ (PSC20E-01200SHU) and I need the GOOD DRIVERS (or Driver pack) for the original Windows 7 x 64, because I can download from the Toshiba site is unusable and it works not!(Display, problem, problem of Blue

  • Integral

    Hi engineers LabVIEW,. I have problems to run a full function between two electric fields E1(x,y) and E2(x,y) in a closed 50 mm radius circle. The electric fields (E1 and E2) values are stored in the different csv file in X, Y and E columns. Can some

  • How can I make a feature to create a table of values of variable size, which I can take the median?

    I am new to LabVIEW all this, I know it's a simple enough question, but: I create a montage where we have two pressure - low pressure and high pressure sensor transducers. The problem is there is noise in the signal I want to reduce - I don't have th