0

What I want is that from the second controller (ControllerScreen1), to be able to modify the text of the button found in the other controller (ControllerDocument). Thank you very much. Here is the code I am using:

package sample;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource( "/sample/multiple_fxml_loading/FXMLDocument.fxml" ));
    primaryStage.setTitle("Test");
    primaryStage.setScene(new Scene(root, 600, 500));
    primaryStage.show();
}
public static void main(String[] args) {
    launch(args);
}
}

This is the Main fxml FXMLDocument.fxml

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

<AnchorPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.multiple_fxml_loading.ControllerDocument">
   <children>
      <BorderPane fx:id="mainPane" >
         <left>
            <VBox alignment="TOP_CENTER" style="-fx-background-color: #AAABBB;" BorderPane.alignment="TOP_CENTER">
               <children>
                  <Button fx:id="button1" mnemonicParsing="false" onAction="#handleButton1" text="Button1">
                     <VBox.margin>
                        <Insets top="20.0" />
                     </VBox.margin>
                  </Button>
                  <Button fx:id="button2"  mnemonicParsing="false" onAction="#handleButton2" text="Button2" />
               </children>
               <BorderPane.margin>
                  <Insets bottom="10.0" left="10.0" top="10.0" />
               </BorderPane.margin>
            </VBox>
         </left>
         <center>
            <Pane fx:id="pane" BorderPane.alignment="CENTER" />
         </center>
         <bottom>
            <TextField fx:id="textFieldMenuContec" text="Menú Contectual" BorderPane.alignment="CENTER" />
         </bottom>
      </BorderPane>
   </children>
</AnchorPane>

Your controller:

package sample.multiple_fxml_loading;
   
public class ControllerDocument implements Initializable {
    @FXML private BorderPane mainPane;    
    @FXML public Button button1, button2;    
    private static Pane paneView1 = null;
    @FXML void handleButton1(ActionEvent event) {

        System.out.println( "Clic en el Boton 1" );
            ControllerMultipleDocument controllerMultipleDocument = new ControllerMultipleDocument();
            Pane paneView = controllerMultipleDocument.getPage( "screen1" );
            paneView1 = controllerMultipleDocument.getPage( "screen1" );
            mainPane.setCenter( paneView );
            variableBooleana = false;
    }
    public void initialize(URL location, ResourceBundle resources) {
    }       
}

Second fxml file, screen1.fxml

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

<AnchorPane style="-fx-background-color: #FF5733;" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.multiple_fxml_loading.ControllerScreen1">
   <children>
      <Label fx:id="labelPantalla" text="Pantalla 1" />
      <Button fx:id="button" mnemonicParsing="false" onAction="#handleButton" text="Button" />
      <JFXTextField fx:id=""  />
   </children>
</AnchorPane>

Your Controller:

package sample.multiple_fxml_loading;
   
public class ControllerScreen1<JFXTextField> {    
    @FXML private Label labelPantalla;   
    @FXML private Button button;    
    @FXML private JFXTextField textField;    
    @FXML private ControllerDocument controllerDocument;

    @FXML
    void handleButton(ActionEvent event) {
        System.out.println( "Clic : " + labelPantalla.getText() );
        controllerDocument.button1.setText("XXX");    
    }
}

And here is the error that throws me:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException ... 48 more Caused by: java.lang.NullPointerException at sample.multiple_fxml_loading.ControllerScreen1.handleButton(ControllerScreen1.java:18) ... 58 more

  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – kleopatra Nov 09 '20 at 15:21
  • controllers must be __loaded__ (vs. instantiated) to inject fields. Unrelated: java naming conventions – kleopatra Nov 09 '20 at 15:23

0 Answers0