javaFX controllers doesn't communicate which each other -


i have problem javafx.i'm doing calculator , divide app between 3 fxml files(1 controller controlls numbers , operators, 2 controller textfield result field, , last 1 should let them communicate each other).

i can not manage how can write own method example put number "3" when press number 3 in textfield-which in other fxml , has own fxml file.there nullpointer exception suppose im not initializing textfield.please me problem.is there way write own method(in example wrote showdigit()) in maincontroller class - method should set text textfield after pressing button - example button 2 put "2" in textfield.

below i've pasted code.

package pl.calculator.controller;  import java.net.url; import java.util.resourcebundle; import javafx.event.actionevent; import javafx.event.eventhandler; import javafx.fxml.fxml; import javafx.fxml.initializable; import javafx.scene.control.button; import javafx.scene.control.textfield; public class mainpanecontroller implements initializable {  @fxml private textpanecontroller textpanecontroller;  @fxml private calculatorpanecontroller calculatorpanecontroller;  @override public void initialize(url location, resourcebundle resources) {      /*calculatorpanecontroller.getbuttonzero().setonaction(new eventhandler<actionevent>() {          @override         public void handle(actionevent event) {              textpanecontroller.gettextfieldexpression().settext("example");         }     });*/  --- < works  } } 

fxml maincontroller

<?xml version="1.0" encoding="utf-8"?>  <?import java.lang.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.layout.anchorpane?>  <vbox xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="pl.calculator.controller.mainpanecontroller"> <children>     <fx:include fx:id="textpane" source="textpane.fxml" />     <fx:include fx:id="calculatorpane" source="calculatorpane.fxml" /> </children> </vbox> 

number , operation controller:

import java.net.url; import java.util.resourcebundle; import javafx.event.actionevent; import javafx.fxml.fxml; import javafx.fxml.initializable;   import javafx.scene.control.button; import javafx.scene.control.textfield; import javafx.scene.layout.gridpane;  public class calculatorpanecontroller implements initializable {  @fxml private button buttonfour;  @fxml private button buttonsix;  @fxml private button buttondivide;  @fxml private button buttonone;  @fxml private button buttonclosebracket;  @fxml private button buttondot;  @fxml private button buttonclear;  @fxml private button buttontwo;  @fxml private button buttonseven;  @fxml private button buttonopenbracket;  @fxml private button buttonthree;  @fxml private button buttonmultiply;  @fxml private button buttonsubtract;  @fxml private button buttoneight;  @fxml private button buttonequal;  @fxml private button buttonnine;  @fxml private button buttonzero;  @fxml private button buttonmemory;  @fxml private button buttonfive;  @fxml private gridpane gridpane;  @fxml private button buttonadd;  @fxml private textpanecontroller textpanecontroller;  @fxml private calculatorpanecontroller calculatorpanecontroller;  @override public void initialize(url location, resourcebundle resources) {   }  @fxml private void showdigit(actionevent event) {      textpanecontroller.gettextfieldexpression().settext("s");  } <---------this 1 doesnt work   public button getbuttonfour() {     return buttonfour; }  public void setbuttonfour(button buttonfour) {     this.buttonfour = buttonfour; }  public button getbuttonsix() {     return buttonsix; }  public void setbuttonsix(button buttonsix) {     this.buttonsix = buttonsix; }  public button getbuttondivide() {     return buttondivide; }  public void setbuttondivide(button buttondivide) {     this.buttondivide = buttondivide; }  public button getbuttonone() {     return buttonone; }  public void setbuttonone(button buttonone) {     this.buttonone = buttonone; }  public button getbuttonclosebracket() {     return buttonclosebracket; }  public void setbuttonclosebracket(button buttonclosebracket) {     this.buttonclosebracket = buttonclosebracket; }  public button getbuttondot() {     return buttondot; }  public void setbuttondot(button buttondot) {     this.buttondot = buttondot; }  public button getbuttonclear() {     return buttonclear; }  public void setbuttonclear(button buttonclear) {     this.buttonclear = buttonclear; }  public button getbuttontwo() {     return buttontwo; }  public void setbuttontwo(button buttontwo) {     this.buttontwo = buttontwo; }  public button getbuttonseven() {     return buttonseven; }  public void setbuttonseven(button buttonseven) {     this.buttonseven = buttonseven; }  public button getbuttonopenbracket() {     return buttonopenbracket; }  public void setbuttonopenbracket(button buttonopenbracket) {     this.buttonopenbracket = buttonopenbracket; }  public button getbuttonthree() {     return buttonthree; }  public void setbuttonthree(button buttonthree) {     this.buttonthree = buttonthree; }  public button getbuttonmultiply() {     return buttonmultiply; }  public void setbuttonmultiply(button buttonmultiply) {     this.buttonmultiply = buttonmultiply; }  public button getbuttonsubtract() {     return buttonsubtract; }  public void setbuttonsubtract(button buttonsubtract) {     this.buttonsubtract = buttonsubtract; }  public button getbuttoneight() {     return buttoneight; }  public void setbuttoneight(button buttoneight) {     this.buttoneight = buttoneight; }  public button getbuttonequal() {     return buttonequal; }  public void setbuttonequal(button buttonequal) {     this.buttonequal = buttonequal; }  public button getbuttonnine() {     return buttonnine; }  public void setbuttonnine(button buttonnine) {     this.buttonnine = buttonnine; }  public button getbuttonzero() {     return buttonzero; }  public void setbuttonzero(button buttonzero) {     this.buttonzero = buttonzero; }  public button getbuttonmemory() {     return buttonmemory; }  public void setbuttonmemory(button buttonmemory) {     this.buttonmemory = buttonmemory; }  public button getbuttonfive() {     return buttonfive; }  public void setbuttonfive(button buttonfive) {     this.buttonfive = buttonfive; }  public gridpane getgridpane() {     return gridpane; }  public void setgridpane(gridpane gridpane) {     this.gridpane = gridpane; }  public button getbuttonadd() {     return buttonadd; }  public void setbuttonadd(button buttonadd) {     this.buttonadd = buttonadd; }  } 

fxml operation , number controller:

<?import java.lang.*?> <?import javafx.geometry.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?>  <gridpane fx:id="gridpane" prefheight="308.0" prefwidth="375.0"    xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"     fx:controller="pl.calculator.controller.calculatorpanecontroller">   <children>   <button fx:id="buttonone" minheight="40.0" minwidth="40.0" mnemonicparsing="false" onaction="#showdigit" text="1" />   <button fx:id="buttonfour" minheight="40.0" minwidth="40.0" mnemonicparsing="false" text="4" gridpane.rowindex="1" />   <button fx:id="buttontwo" minheight="40.0" minwidth="40.0" mnemonicparsing="false" text="2" gridpane.columnindex="1" />   <button fx:id="buttonseven" minheight="40.0" minwidth="40.0" mnemonicparsing="false" text="7" gridpane.rowindex="2" />   <button fx:id="buttonfive" minheight="40.0" minwidth="40.0" mnemonicparsing="false" text="5" gridpane.columnindex="1" gridpane.rowindex="1" />   <button fx:id="buttoneight" minheight="40.0" minwidth="40.0" mnemonicparsing="false" text="8" gridpane.columnindex="1" gridpane.rowindex="2" />   <button fx:id="buttonthree" minheight="40.0" minwidth="40.0" mnemonicparsing="false" text="3" gridpane.columnindex="2" />   <button fx:id="buttonsix" minheight="40.0" minwidth="40.0" mnemonicparsing="false" text="6" gridpane.columnindex="2" gridpane.rowindex="1" />   <button fx:id="buttonnine" minheight="40.0" minwidth="40.0" mnemonicparsing="false" text="9" gridpane.columnindex="2" gridpane.rowindex="2" />   <button fx:id="buttonzero" minheight="40.0" minwidth="40.0" mnemonicparsing="false" text="0" gridpane.rowindex="3" />   <button fx:id="buttondot" minheight="40.0" minwidth="40.0" mnemonicparsing="false" text="." gridpane.columnindex="1" gridpane.rowindex="3" />   <button fx:id="buttonequal" minheight="40.0" minwidth="40.0" mnemonicparsing="false" text="=" gridpane.columnindex="2" gridpane.rowindex="3" />   <button fx:id="buttondivide" minheight="40.0" minwidth="40.0" mnemonicparsing="false" text="/" gridpane.columnindex="3" gridpane.rowindex="2" />   <button fx:id="buttonmultiply" minheight="40.0" minwidth="40.0" mnemonicparsing="false" text="*" gridpane.columnindex="3" gridpane.rowindex="3" />   <button fx:id="buttonsubtract" minheight="40.0" minwidth="40.0" mnemonicparsing="false" text="-" gridpane.columnindex="3" gridpane.rowindex="1" />   <button fx:id="buttonadd" minheight="40.0" minwidth="40.0" mnemonicparsing="false" text="+" gridpane.columnindex="3" />   <button fx:id="buttonclosebracket" minheight="40.0" minwidth="40.0" mnemonicparsing="false" text=")" gridpane.columnindex="4" gridpane.rowindex="3" />   <button fx:id="buttonopenbracket" minheight="40.0" minwidth="40.0" mnemonicparsing="false" text="(" gridpane.columnindex="4" gridpane.rowindex="2" />   <button fx:id="buttonmemory" minheight="40.0" minwidth="40.0" mnemonicparsing="false" text="m" gridpane.columnindex="4" />   <button fx:id="buttonclear" minheight="40.0" minwidth="40.0" mnemonicparsing="false" text="c" gridpane.columnindex="4" gridpane.rowindex="1" />    </children>    <columnconstraints>    <columnconstraints hgrow="sometimes" minwidth="10.0" prefwidth="100.0" />    <columnconstraints hgrow="sometimes" minwidth="10.0" prefwidth="100.0" />    <columnconstraints hgrow="sometimes" minwidth="10.0" prefwidth="100.0" />     <columnconstraints hgrow="sometimes" minwidth="10.0" prefwidth="100.0" />     <columnconstraints hgrow="sometimes" minwidth="10.0" prefwidth="100.0" />    </columnconstraints>    <rowconstraints>    <rowconstraints minheight="10.0" prefheight="30.0" vgrow="sometimes" />     <rowconstraints minheight="10.0" prefheight="30.0" vgrow="sometimes" />     <rowconstraints minheight="10.0" prefheight="30.0" vgrow="sometimes" />     <rowconstraints minheight="10.0" prefheight="30.0" vgrow="sometimes" />     </rowconstraints>     </gridpane> 

controller textfields(results)

import java.net.url; import java.util.resourcebundle; import javafx.fxml.fxml; import javafx.fxml.initializable; import javafx.scene.control.label; import javafx.scene.control.textfield;  public class textpanecontroller {  @fxml private label labelexpression;  @fxml private textfield textfieldonp;  @fxml private label labelonp;  @fxml private textfield textfieldexpression;   public label getlabelexpression() {     return labelexpression; }  public void setlableexpression(label lableexpression) {     this.labelexpression = lableexpression; }  public textfield gettextfieldonp() {     return textfieldonp; }  public void settextfieldonp(textfield textfieldonp) {     this.textfieldonp = textfieldonp; }  public label getlabelonp() {     return labelonp; }  public void setlabelonp(label labelonp) {     this.labelonp = labelonp; }  public textfield gettextfieldexpression() {     return textfieldexpression; }  public void settextfieldexpression(textfield textfieldexpression) {     this.textfieldexpression = textfieldexpression; } } 

fxml file text:

<?import javafx.geometry.*?> <?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.layout.anchorpane?>   <anchorpane maxheight="-infinity" maxwidth="-infinity" minheight="-infinity" minwidth="-infinity" prefheight="89.0" prefwidth="349.0"  xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="pl.calculator.controller.textpanecontroller">   <children>   <label fx:id="lableexpression" layoutx="21.0" layouty="14.0" text="wprowadź wyrażenie:" />   <label fx:id="labelonp" layoutx="213.0" layouty="14.0" text="wyrażenie onp" />     <textfield fx:id="textfieldexpression" layoutx="5.0" layouty="45.0" />     <textfield fx:id="textfieldonp" editable="false" layoutx="180.0" layouty="45.0" /> </children> </anchorpane> 

and stack trace:

textfield[id=textfieldexpression, styleclass=text-input text-field] exception in thread "javafx application thread" java.lang.runtimeexception: java.lang.reflect.invocationtargetexception     @ javafx.fxml.fxmlloader$methodhandler.invoke(unknown source)     @ javafx.fxml.fxmlloader$controllermethodeventhandler.handle(unknown source)     @ com.sun.javafx.event.compositeeventhandler.dispatchbubblingevent(unknown source)     @ com.sun.javafx.event.eventhandlermanager.dispatchbubblingevent(unknown source)     @ com.sun.javafx.event.eventhandlermanager.dispatchbubblingevent(unknown source)     @ com.sun.javafx.event.compositeeventdispatcher.dispatchbubblingevent(unknown source)     @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(unknown source)     @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(unknown source)     @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(unknown source)     @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(unknown source)     @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(unknown source)     @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(unknown source)     @ com.sun.javafx.event.eventutil.fireeventimpl(unknown source)     @ com.sun.javafx.event.eventutil.fireevent(unknown source)     @ javafx.event.event.fireevent(unknown source)     @ javafx.scene.node.fireevent(unknown source)     @ javafx.scene.control.button.fire(unknown source)     @ com.sun.javafx.scene.control.behavior.buttonbehavior.mousereleased(unknown source)     @ com.sun.javafx.scene.control.skin.behaviorskinbase$1.handle(unknown source)     @ com.sun.javafx.scene.control.skin.behaviorskinbase$1.handle(unknown source)     @ com.sun.javafx.event.compositeeventhandler$normaleventhandlerrecord.handlebubblingevent(unknown source)     @ com.sun.javafx.event.compositeeventhandler.dispatchbubblingevent(unknown source)     @ com.sun.javafx.event.eventhandlermanager.dispatchbubblingevent(unknown source)     @ com.sun.javafx.event.eventhandlermanager.dispatchbubblingevent(unknown source)     @ com.sun.javafx.event.compositeeventdispatcher.dispatchbubblingevent(unknown source)     @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(unknown source)     @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(unknown source)     @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(unknown source)     @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(unknown source)     @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(unknown source)     @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(unknown source)     @ com.sun.javafx.event.eventutil.fireeventimpl(unknown source)     @ com.sun.javafx.event.eventutil.fireevent(unknown source)     @ javafx.event.event.fireevent(unknown source)     @ javafx.scene.scene$mousehandler.process(unknown source)     @ javafx.scene.scene$mousehandler.access$1800(unknown source)     @ javafx.scene.scene.impl_processmouseevent(unknown source)     @ javafx.scene.scene$scenepeerlistener.mouseevent(unknown source)     @ com.sun.javafx.tk.quantum.glassvieweventhandler$mouseeventnotification.run(unknown source)     @ com.sun.javafx.tk.quantum.glassvieweventhandler$mouseeventnotification.run(unknown source)     @ java.security.accesscontroller.doprivileged(native method)     @ com.sun.javafx.tk.quantum.glassvieweventhandler.handlemouseevent(unknown source)     @ com.sun.glass.ui.view.handlemouseevent(unknown source)     @ com.sun.glass.ui.view.notifymouse(unknown source)     @ com.sun.glass.ui.win.winapplication._runloop(native method)     @ com.sun.glass.ui.win.winapplication.access$300(unknown source)     @ com.sun.glass.ui.win.winapplication$4$1.run(unknown source)     @ java.lang.thread.run(unknown source) caused by: java.lang.reflect.invocationtargetexception     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)     @ sun.reflect.nativemethodaccessorimpl.invoke(unknown source)     @ sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source)     @ java.lang.reflect.method.invoke(unknown source)     @ sun.reflect.misc.trampoline.invoke(unknown source)     @ sun.reflect.generatedmethodaccessor1.invoke(unknown source)     @ sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source)     @ java.lang.reflect.method.invoke(unknown source)     @ sun.reflect.misc.methodutil.invoke(unknown source)     ... 48 more caused by: java.lang.nullpointerexception     @ pl.calculator.controller.calculatorpanecontroller.showdigit(calculatorpanecontroller.java:89)     ... 57 more 

the fxml file calculatorpane.fxml doesn't have <fx:include fx:id="textpane" ... />, associated controller doesn't textpanecontroller injected it. textpanecontroller in calculatorpanecontroller null, , nullpointerexception.

a way update value calculatorpanecontroller give both controllers shared data model, , update model. like

public class datamodel {      private final stringproperty text = new simplestringproperty();      public stringproperty textproperty() {         return text ;     }      public final string gettext() {         return textproperty().get();     }      public final void settext(string text) {         textproperty().set(text);     }      // other properties needed... } 

then controllers can do

public class textpanecontroller {      private datamodel model ;      @fxml     private textfield textfieldexpression ;      // etc ...      public void setmodel(datamodel model) {         this.model = model ;         textfieldexpression.textproperty().bindbidirectional(model.textproperty());     } } 

and

public class calculatorpanecontroller {      private datamodel model ;      public void setmodel(datamodel model) {         this.model = model ;     }      // ...      @fxml     private void showdigit(actionevent event) {          model.settext("s");      } }      

finally, tie in main controller's initialize method:

public class mainpanecontroller implements initializable {      @fxml     private textpanecontroller textpanecontroller;      @fxml     private calculatorpanecontroller calculatorpanecontroller;      @override     public void initialize(url location, resourcebundle resources) {          datamodel model = new datamodel();         textpanecontroller.setmodel(model);         calculatorpanecontroller.setmodel(model);      }  }  

you don't need (and shouldn't have) get/set methods controls in controllers. should kept private , not exposed outside controller.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -