JavaFX Communication Between Controllers

When you begin with JavaFX, one problem you might come up with is the communication between different controllers to communicate between scenes. In this article, we will see how to properly communicate between two different controllers in JavaFX.

JavaFX Controller Communication Method

The solution here is to get the controller from FXMLLoader. We use JavaFX FXMLLoader for inflating fxml and loading new views. This loader has a method called getController(). getController method will return an instance of the controller corresponding to the view we are going to load.

In this example, we take input to a text field from the user. When a button is clicked, new window will be opened and we pass the typed text to new window.

First Scene Controller

When the button actionBtn is clicked, a new window will be called which is the second scene window. We take the input from inputField textfield and pass it with transfer data.

public class Scene1Controller implements Initializable {

    @FXML
    private TextField inputField;
    @FXML
    private Button actionBtn;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        //When button clicked, load window and pass data
        actionBtn.setOnAction(event -> {
            loadSceneAndSendMessage();
        });
    }

    private void loadSceneAndSendMessage() {
        try {
            //Load second scene
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/javafxcontrollercommunication/scene2/scene2.fxml"));
            Parent root = loader.load();

            //Get controller of scene2
            Scene2Controller scene2Controller = loader.getController();
            //Pass whatever data you want. You can have multiple method calls here
            scene2Controller.transferMessage(inputField.getText());

            //Show scene 2 in new window
            Stage stage = new Stage();
            stage.setScene(new Scene(root));
            stage.setTitle("Second Window");
            stage.show();
        } catch (IOException ex) {
            System.err.println(ex);
        }
    }
}

Second Scene Controller

This controller is elementary. You just have to write a method that accepts the string. Once the string is received, we set that in the display TextField.

public class Scene2Controller {
    @FXML
    private TextField display;

    //Receive message from scene 1
    public void transferMessage(String message) {
        //Display the message
        display.setText(message);
    }
}

Watch another example on YouTube


You may also find following interesting:-

View in Github
[JavaFX communicate between scenes, JavaFX window communication. JavaFX share data between scenes]

Muhammed Afsal Villan

Muhammed Afsal Villan is an experienced full-stack developer, specialized in desktop and mobile application development. He also regularly publishes quality tutorials on his YouTube channel named 'Genuine Coder'. He likes to contribute to open-source projects and is always enthusiastic about new technologies.

More From Author

JavaFX Splash Screen

JavaFX 3D Tutorial #5 – More fine mouse control – Zoom in and Zoom out

924 thoughts on “JavaFX Communication Between Controllers

  1. Pingback: lasweb.iwopop.com
  2. Pingback: kalwer.micro.blog
  3. Pingback: cialis
  4. Pingback: kswbnh.nethouse.ru
  5. Pingback: buy cialis delhi
  6. Pingback: canadian viagra
  7. Pingback: pornoizle}
  8. Pingback: buy viagra germany
  9. Pingback: buy viagra pills
  10. Pingback: buy viagra
  11. Pingback: canadian drug
  12. Pingback: canadian drugs
  13. Pingback: buy viagra 25mg
  14. Pingback: dose of stromectol
  15. Pingback: online pharmacy
  16. Pingback: online drug store
  17. Pingback: cialis from canada
  18. Pingback: buy viagra cheap
  19. Pingback: stromectol generic
  20. Pingback: canadadrugs
  21. Pingback: Northwest Pharmacy
  22. Pingback: buy viagra usa
  23. Pingback: Northwest Pharmacy
  24. Pingback: logarkomx
  25. Pingback: Bahiscom
  26. Pingback: Betmatik
  27. Pingback: Betist
  28. Pingback: Cratosslot.
  29. Pingback: Betlike
  30. Pingback: Betebet
  31. Pingback: Mariobet
  32. Pingback: Tempobet
  33. Pingback: Tipobet
  34. Pingback: Klasbahis
  35. Pingback: Vdcasino
  36. Pingback: Casinoeuro
  37. Pingback: imajbet
  38. Pingback: imajbet giris
  39. Pingback: Sahabet
  40. Pingback: 1xbet
  41. Pingback: Bahigo
  42. Pingback: Bahis siteleri
  43. Pingback: Onwin
  44. Pingback: Kralbet
  45. Pingback: Tipobet Giriş
  46. Pingback: Betkolik
  47. Pingback: Casino Siteleri
  48. Pingback: Bettilt
  49. Pingback: Betasus
  50. Pingback: Dinamobet
  51. Pingback: Jojobet
  52. Pingback: Jojobet giriş
  53. Pingback: Hepsibahis
  54. Pingback: Marsbahis
  55. Pingback: stromectol tablets
  56. Pingback: stromectol sale
  57. Pingback: canada medication
  58. Pingback: canada viagra
  59. Pingback: canadian cialis
  60. Pingback: canada drug
  61. Pingback: canadian drugs
  62. Pingback: viagra canada
  63. Pingback: ed drugs generic
  64. Pingback: drugstore online
  65. Pingback: canadian cialis
  66. Pingback: canadian viagra
  67. Pingback: canadian pharmacys
  68. Pingback: canadian drugstore
  69. Pingback: pharmacy
  70. Pingback: drugstore online
  71. Pingback: madridbet
  72. Pingback: madridbet
  73. Pingback: canadadrugs
  74. Pingback: pharmacy canada
  75. Pingback: Northwest Pharmacy
  76. Pingback: www.dibiz.comgdooc
  77. Pingback: northwestpharmacy
  78. Pingback: canada drug
  79. Pingback: drugstore online
  80. Pingback: canada rx
  81. Pingback: madridbet
  82. Pingback: meritking
  83. Pingback: meritking
  84. Pingback: meritking
  85. Pingback: grandpashabet
  86. Pingback: meritking
  87. Pingback: meritking giriş
  88. Pingback: Autoapprove List
  89. Pingback: grandpashabet
  90. Pingback: fuck google
  91. Pingback: porn
  92. Pingback: fuck
  93. Pingback: porn
  94. Pingback: porn
  95. Pingback: buy essay writing
  96. Pingback: save rx pharmacy
  97. Pingback: tadalafil benefits
  98. Pingback: buy genuine viagra
  99. Pingback: lisinopril allegra
  100. Pingback: viagra shop
  101. Pingback: child porn
  102. Pingback: grandpashabet
  103. Pingback: porn
  104. Pingback: grandpashabet
  105. Pingback: porno izle
  106. Pingback: child porn
  107. Pingback: cratosroyalbet
  108. Pingback: celecoxib high
  109. Pingback: baclofen addiction
  110. Pingback: buspar alcohol
  111. Pingback: child porn
  112. Pingback: acarbose suppliers
  113. Pingback: porn
  114. Pingback: stromectol pill
  115. Pingback: protonix liquid
  116. Pingback: synthroid testing
  117. Pingback: child porn
  118. Pingback: child porn
  119. Pingback: child porn
  120. Pingback: sex historie
  121. Pingback: stromectol buy
  122. Pingback: anan脹n am脹
  123. Pingback: porn
  124. Pingback: child porn
  125. Pingback: child porn
  126. Pingback: child porn
  127. Pingback: child porn
  128. Pingback: child porn
  129. Pingback: cipro.co.za
  130. Pingback: spinco
  131. Pingback: porn
  132. Pingback: Cocuk pornosu
  133. Pingback: child porn
  134. Pingback: child porn
  135. Pingback: child porn
  136. Pingback: porn
  137. Pingback: fuck
  138. Pingback: child porn
  139. Pingback: anal porno
  140. Pingback: child porn
  141. Pingback: sex
  142. Pingback: spam
  143. Pingback: porn
  144. Pingback: porn
  145. Pingback: child porn
  146. Pingback: child porn
  147. Pingback: child porn
  148. Pingback: iporn
  149. Pingback: child porn
  150. Pingback: porn
  151. Pingback: porn
  152. Pingback: porn
  153. Pingback: meritking
  154. Pingback: meritking
  155. Pingback: meritking
  156. Pingback: meritking
  157. Pingback: meritking
  158. Pingback: meritking
  159. Pingback: spam
  160. Pingback: child porn
  161. Pingback: porn
  162. Pingback: Konya SEO Uzman脹
  163. Pingback: porn
  164. Pingback: spam
  165. Pingback: web sitesi kurma
  166. Pingback: child porn
  167. Pingback: porn
  168. Pingback: child porn
  169. Pingback: child porn
  170. Pingback: child porn
  171. Pingback: child porn
  172. Pingback: porn
  173. Pingback: child porn
  174. Pingback: child porn
  175. Pingback: porn
  176. Pingback: porn
  177. Pingback: porn
  178. Pingback: porn
  179. Pingback: child porn
  180. Pingback: tadalafil 50mg
  181. Pingback: levitra vardenafil
  182. Pingback: porn
  183. Pingback: child porn
  184. Pingback: diclofenac dose
  185. Pingback: mebeverine pka
  186. Pingback: imdur xl
  187. Pingback: baclofen powder
  188. Pingback: child porn
  189. Pingback: porn
  190. Pingback: child porn
  191. Pingback: child porn
  192. Pingback: child porn
  193. Pingback: child porn
  194. Pingback: child porn
  195. Pingback: child porn
  196. Pingback: child porn
  197. Pingback: child porn
  198. Pingback: child porn
  199. Pingback: porn
  200. Pingback: grandpashabet
  201. Pingback: child porn
  202. Pingback: child porn
  203. Pingback: grandpashabet
  204. Pingback: fuck google
  205. Pingback: child porn
  206. Pingback: porn
  207. Pingback: spam
  208. Pingback: kingroyal porna
  209. Pingback: child porn
  210. Pingback: porn
  211. Pingback: child porn
  212. Pingback: child porn
  213. Pingback: kingroyal porn
  214. Pingback: spam
  215. Pingback: child porn
  216. Pingback: child porn
  217. Pingback: child porn
  218. Pingback: orosbu cocugusun
  219. Pingback: child porn
  220. Pingback: child porn
  221. Pingback: Jeffrey Epstein
  222. Pingback: child porn
  223. Pingback: spam

Leave a Reply