2

I am writing an app (virtual clinic) and I don't know how can I inject FXML files (inject in MainController.FXML), which will be display after click the button, earlier I injected it between Button's line in Main.FXML, but It doesn't work, I mean it does work, but then included FXML is visible all time, and I want in order to CheckOutPatient.FXML will be visible when I click "checkoutButton" in Main.FXML, in space "optionPane", here is a code:

Main.FXML:

<BorderPane fx:id="main" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.MainController">
  <center>
     <Pane prefHeight="530.0" prefWidth="750.0" BorderPane.alignment="CENTER">
       <children>
        <ImageView fitHeight="539.0" fitWidth="953.0" layoutX="-196.0" layoutY="-4.0">
           <image>
              <Image url="@../../../../../Users/Damian/Desktop/asdffff.png" />
           </image>
        </ImageView>
        <Rectangle arcHeight="5.0" arcWidth="5.0" fill="#89a1b9" height="540.0" layoutX="-6.0" layoutY="-3.0" opacity="0.94" stroke="WHITE" strokeType="INSIDE" width="154.0" />
        <Button fx:id="registrationButton" layoutX="5.0" layoutY="72.0" mnemonicParsing="false" onAction="#chooseOption" prefHeight="27.0" prefWidth="137.0" style="-fx-background-color: #89a1b9; -fx-border-color: WHITE;" text="Zarejestruj pacjenta">
           <cursor>
              <Cursor fx:constant="HAND" />
           </cursor>
        </Button>
        <Button fx:id="infopatientButton" layoutX="5.0" layoutY="122.0" mnemonicParsing="false" onAction="#chooseOption" style="-fx-background-color: #89a1b9; -fx-border-color: WHITE;" text="Informacje o pacjencie">
           <cursor>
              <Cursor fx:constant="HAND" />
           </cursor>
        </Button>
        <Button fx:id="checkoutButton" layoutX="5.0" layoutY="172.0" mnemonicParsing="false" onAction="#chooseOption" prefHeight="27.0" prefWidth="137.0" style="-fx-background-color: #89a1b9; -fx-border-color: WHITE;" text="Wypisz pacjenta">
           <cursor>
              <Cursor fx:constant="HAND" />
           </cursor>
        </Button>
        <Button fx:id="medicalstaffButton" layoutX="5.0" layoutY="225.0" mnemonicParsing="false" onAction="#chooseOption" prefHeight="27.0" prefWidth="137.0" style="-fx-background-color: #89a1b9; -fx-border-color: WHITE;" text="Personel medyczny">
           <cursor>
              <Cursor fx:constant="HAND" />
           </cursor>
        </Button>
        <Button fx:id="usersButton" layoutX="5.0" layoutY="275.0" mnemonicParsing="false" onAction="#chooseOption" prefHeight="27.0" prefWidth="137.0" style="-fx-background-color: #89a1b9; -fx-border-color: WHITE;" text="Użytkownicy">
           <cursor>
              <Cursor fx:constant="HAND" />
           </cursor>
        </Button>
        <Button fx:id="logoutButton" layoutX="5.0" layoutY="325.0" mnemonicParsing="false" onAction="#chooseOption" prefHeight="27.0" prefWidth="64.0" style="-fx-background-color: #89a1b9; -fx-border-color: WHITE;" text="Wyloguj">
           <cursor>
              <Cursor fx:constant="HAND" />
           </cursor>
        </Button>
        <Button fx:id="exitButton" layoutX="78.0" layoutY="325.0" mnemonicParsing="false" onAction="#chooseOption" prefHeight="27.0" prefWidth="63.0" style="-fx-background-color: #89a1b9; -fx-border-color: WHITE;" text="Zamknij">
           <cursor>
              <Cursor fx:constant="HAND" />
           </cursor>
        </Button>
        <ImageView fitHeight="200.0" fitWidth="200.0" layoutX="550.0" layoutY="350.0">
           <image>
              <Image url="@../../../../../Users/Damian/Desktop/imagesff.png" />
           </image>
        </ImageView>
        <Label layoutX="36.0" layoutY="14.0" prefHeight="37.0" prefWidth="489.0" text="MENU" wrapText="true">
           <font>
              <Font size="28.0" />
           </font>
        </Label>
        <AnchorPane fx:id="optionPane" layoutX="162.0" layoutY="16.0" prefHeight="497.0" prefWidth="572.0" />
     </children>
     </Pane>
  </center>
</BorderPane>

CheckOutPatient.FXML:

<BorderPane fx:id="checkoutpatient" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.CheckOutPatientController">
    <center>
     <Pane prefHeight="497.0" prefWidth="572.0" BorderPane.alignment="CENTER">
      <children>
        <VBox layoutX="-1.0" prefHeight="450.0" prefWidth="112.0" />
        <Pane opacity="0.85" prefHeight="497.0" prefWidth="572.0" style="-fx-background-color: #81a1b9;">
           <children>
              <TextField layoutX="247.0" layoutY="377.0" prefHeight="25.0" prefWidth="115.0" />
              <TextField layoutX="247.0" layoutY="337.0" prefHeight="25.0" prefWidth="116.0" />
              <TextField layoutX="247.0" layoutY="413.0" prefHeight="25.0" prefWidth="116.0" />
              <TextField layoutX="230.0" layoutY="255.0" prefHeight="25.0" prefWidth="176.0" />
              <Label layoutX="152.0" layoutY="380.0" text="Rodzaj pokoju:">
                 <font>
                    <Font size="13.0" />
                 </font>
              </Label>
              <Label layoutX="152.0" layoutY="340.0" text="Numer pokoju:">
                 <font>
                    <Font size="13.0" />
                 </font>
              </Label>
              <Label layoutX="152.0" layoutY="416.0" text="Data rejestracji:">
                 <font>
                    <Font size="13.0" />
                 </font>
              </Label>
              <Label layoutX="152.0" layoutY="21.0" text="PID:">
                 <font>
                    <Font size="13.0" />
                 </font>
              </Label>
              <Label layoutX="152.0" layoutY="62.0" text="Imię:">
                 <font>
                    <Font size="13.0" />
                 </font>
              </Label>
              <Label layoutX="152.0" layoutY="102.0" text="Nazwisko:">
                 <font>
                    <Font size="13.0" />
                 </font>
              </Label>
              <Label layoutX="152.0" layoutY="137.0" text="Płeć:">
                 <font>
                    <Font size="13.0" />
                 </font>
              </Label>
              <Label layoutX="152.0" layoutY="177.0" text="Wiek:">
                 <font>
                    <Font size="13.0" />
                 </font>
              </Label>
              <Label layoutX="152.0" layoutY="218.0" text="Telefon:">
                 <font>
                    <Font size="13.0" />
                 </font>
              </Label>
              <Label layoutX="152.0" layoutY="258.0" text="Adres:">
                 <font>
                    <Font size="13.0" />
                 </font>
              </Label>
              <Label layoutX="152.0" layoutY="299.0" text="Dolegliwość:">
                 <font>
                    <Font size="13.0" />
                 </font>
              </Label>
              <TextField fx:id="infodiseaseField" layoutX="231.0" layoutY="299.0" onAction="#enteredInfoAboutPatient" prefHeight="25.0" prefWidth="136.0" />
              <TextField layoutX="-41.0" layoutY="564.0" prefHeight="25.0" prefWidth="267.0" />
              <TextField fx:id="infophonenumberField" layoutX="231.0" layoutY="218.0" onAction="#enteredInfoAboutPatient" prefHeight="25.0" prefWidth="138.0" />
              <TextField fx:id="infoageField" layoutX="231.0" layoutY="177.0" onAction="#enteredInfoAboutPatient" prefHeight="25.0" prefWidth="138.0" />
              <TextField fx:id="infogenderField" layoutX="231.0" layoutY="137.0" onAction="#enteredInfoAboutPatient" prefHeight="25.0" prefWidth="139.0" />
              <TextField fx:id="infolastNameField" layoutX="231.0" layoutY="102.0" onAction="#enteredInfoAboutPatient" prefHeight="25.0" prefWidth="140.0" />
              <TextField fx:id="infonameField" layoutX="231.0" layoutY="62.0" onAction="#enteredInfoAboutPatient" prefHeight="25.0" prefWidth="140.0" />
              <TextField fx:id="infoPIDField" layoutX="231.0" layoutY="21.0" onAction="#enteredInfoAboutPatient" prefHeight="25.0" prefWidth="141.0" />
              <ListView layoutX="25.0" layoutY="18.0" prefHeight="460.0" prefWidth="117.0" />
              <Label layoutX="152.0" layoutY="450.0" text="Data wypisania:">
                 <font>
                    <Font size="13.0" />
                 </font>
              </Label>
              <TextField layoutX="247.0" layoutY="447.0" prefHeight="25.0" prefWidth="116.0" />
              <MenuButton layoutX="484.0" layoutY="59.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="63.0">
                <items>
                  <MenuItem fx:id="goodCondition" mnemonicParsing="false" onAction="#chooseCondition" text="zdrowy" />
                  <MenuItem fx:id="uncertainCondition" mnemonicParsing="false" onAction="#chooseCondition" text="niepewny" />
                </items>
              </MenuButton>
              <Label layoutX="390.0" layoutY="62.0" text="Stan pacjenta:">
                 <font>
                    <Font size="13.0" />
                 </font>
              </Label>
              <Label layoutX="412.0" layoutY="137.0" text="pobyt:">
                 <font>
                    <Font size="13.0" />
                 </font>
              </Label>
              <Label layoutX="380.0" layoutY="177.0" text="wyżywienie:">
                 <font>
                    <Font size="13.0" />
                 </font>
              </Label>
              <TextField layoutX="463.0" layoutY="134.0" prefHeight="25.0" prefWidth="66.0" />
              <TextField layoutX="463.0" layoutY="215.0" prefHeight="25.0" prefWidth="66.0" />
              <TextField layoutX="463.0" layoutY="174.0" prefHeight="25.0" prefWidth="66.0" />
              <Label layoutX="398.0" layoutY="218.0" text="obsługa:">
                 <font>
                    <Font size="13.0" />
                 </font>
              </Label>
              <TextField layoutX="463.0" layoutY="255.0" prefHeight="25.0" prefWidth="67.0" />
              <Button fx:id="confirmButton" layoutX="424.0" layoutY="319.0" mnemonicParsing="false" prefHeight="36.0" prefWidth="100.0" text="Zatwierdź" />
              <Button fx:id="exitcheckButton" layoutX="424.0" layoutY="372.0" mnemonicParsing="false" prefHeight="36.0" prefWidth="100.0" text="Zamknij" />
              <Label layoutX="470.0" layoutY="103.0" text="Koszty">
                 <font>
                    <Font size="14.0" />
                 </font>
              </Label>
           </children></Pane>
       </children>
     </Pane>
   </center>
</BorderPane>

RegistrationController:

@FXML
public void handleAddPatient(ActionEvent event) {

    infoPatientController.infoageField.setText("asdf");
 }

Here is error:

Exception in thread "JavaFX Application Thread"  java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown  Source)
at  com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Node.fireEvent(Unknown Source)
at javafx.scene.control.Button.fire(Unknown Source)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at   com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
... 49 more
  Caused by: java.lang.NullPointerException
at   controller.RegistrationPatientController.handleAddPatient(RegistrationPatientController.java:228)
dante
  • 393
  • 1
  • 15
  • 31
  • Ahh..You've got to "love" those robo-reviewers that just upvote because it looks like something like code is in the question.Your code is not what I'd call [minimal](http://stackoverflow.com/help/mcve).All those Labels, TextFields, Buttons ect. are not really relevant and fonts and cursors seem to be irrelevant too.Something that seems relevant but is not included in the question are the controllers.Also what do you mean by "Inject FXML". Injection in the FXML context normally refers to asigning instances of objects to controller fields. Please improve your description of the desired behavior. – fabian Dec 18 '15 at 16:39
  • @fabian I managed it already, but my main problem is that my controllers dont want to communicate with each other, in RegistrationController I create a method (click button) and after call this method I wanted to display text in different FXML file (InfoPatient.fxml), which is managing by InfoPatientController. I paste another code in post, please check it. – dante Dec 18 '15 at 16:56
  • This post should help: http://stackoverflow.com/a/14190310/2991525 . Maybe also: http://stackoverflow.com/q/12543487/2991525 . But for future posts you really need to learn how to identify the relevant parts of your code and remove the irrelevant parts: You have an issue with a single `Node`/controller. We don't really need to know about the 20+ other nodes. Also you still haven't included the part that assigns the fields that cause the NPE or checked, if `infoPatientController` or `infoageField` are `null`. Minimal questions are much easier to answer: http://stackoverflow.com/help/mcve – fabian Dec 18 '15 at 17:15

1 Answers1

-1

Try the following code:

// creating a loader for the scene you want to change
FXMLLoader loader = new FXMLLoader(getClass().getResource("yourScene.fxml"));

// creating a controller to be called to customize that scene's elements
MainController controller = (MainController)loader.getController();

Now you can do things like:

controller.yourButton.setText("...");

Hope this helps!

Filippos Zofakis
  • 561
  • 6
  • 12