java - Adding mouse click action events to labels in JavaFX with FXML -
i making version of risk in javafx. right display game data i'm using grid of labels display information each territory. to, now, have mouse click listeners on labels can test game functions clicking on them until have full game board made. except when try add listeners in fxml document errors. right i'm declaring labels in controller, adding them collection can use loop bind each label's textproperty, adding each grid. in fxml i'm defining onmouseclicked each. here controller code:
@fxml private gridpane tgrid = new gridpane(); @fxml label label1 = new label(); @fxml label label2 = new label(); @fxml label label3 = new label(); @fxml label label4 = new label(); @fxml label label5 = new label(); @fxml label label6 = new label(); @fxml label label7 = new label(); @fxml label label8 = new label(); @fxml label label9 = new label(); @fxml label label10 = new label(); @fxml label label11 = new label(); @fxml label label12 = new label(); @fxml label label13 = new label(); @fxml label label14 = new label(); @fxml label label15 = new label(); @fxml label label16 = new label(); @fxml label label17 = new label(); @fxml label label18 = new label(); @fxml label label19 = new label(); @fxml label label20 = new label(); @fxml label label21 = new label(); @fxml label label22 = new label(); @fxml label label23 = new label(); @fxml label label24 = new label(); @fxml label label25 = new label(); @fxml label label26 = new label(); @fxml label label27 = new label(); @fxml label label28 = new label(); @fxml label label29 = new label(); @fxml label label30 = new label(); @fxml label label31 = new label(); @fxml label label32 = new label(); @fxml label label33 = new label(); @fxml label label34 = new label(); @fxml label label35 = new label(); @fxml label label36 = new label(); @fxml label label37 = new label(); @fxml label label38 = new label(); @fxml label label39 = new label(); @fxml label label40 = new label(); @fxml label label41 = new label(); @fxml label label42 = new label(); @override public void initialize(url url, resourcebundle rb) { anchor.setleftanchor(tgrid, 10.0); anchor.setrightanchor(pgrid, 10.0); tgrid.setvisible(false); pgrid.setvisible(false); labels.add(label1); labels.add(label2); labels.add(label3); labels.add(label4); labels.add(label5); labels.add(label6); labels.add(label7); labels.add(label8); labels.add(label9); labels.add(label10); labels.add(label11); labels.add(label12); labels.add(label13); labels.add(label14); labels.add(label15); labels.add(label16); labels.add(label17); labels.add(label18); labels.add(label19); labels.add(label20); labels.add(label21); labels.add(label22); labels.add(label23); labels.add(label24); labels.add(label25); labels.add(label26); labels.add(label27); labels.add(label28); labels.add(label29); labels.add(label30); labels.add(label31); labels.add(label32); labels.add(label33); labels.add(label34); labels.add(label35); labels.add(label36); labels.add(label37); labels.add(label38); labels.add(label39); labels.add(label40); labels.add(label41); labels.add(label42); int k = 0; for(int = 0; < 6; i++) { for(int j = 0; j < game.getcontinent(i).getterritorynum(); j++) { labels.get(k).textproperty().bind(game.getcontinent(i).getterritory(j).getproperty()); k += 1; }//for }//for (int = 0; < 42; i++) { tgrid.add(labels.get(i), 0, i); }//for
and here fxml code (except labels have onmouseclicked):
<label fx:id="label1"onmouseclicked="#labelaction" /> <label fx:id="label2"/> <label fx:id="label3"/> <label fx:id="label4"/> <label fx:id="label5"/> <label fx:id="label6"/> <label fx:id="label7"/> <label fx:id="label8"/> <label fx:id="label9"/> <label fx:id="label10"/> <label fx:id="label11"/> <label fx:id="label12"/> <label fx:id="label13"/> <label fx:id="label14"/> <label fx:id="label15"/> <label fx:id="label16"/> <label fx:id="label17"/> <label fx:id="label18"/> <label fx:id="label19"/> <label fx:id="label20"/> <label fx:id="label21"/> <label fx:id="label22"/> <label fx:id="label23"/> <label fx:id="label24"/> <label fx:id="label25"/> <label fx:id="label26"/> <label fx:id="label27"/> <label fx:id="label28"/> <label fx:id="label29"/> <label fx:id="label30"/> <label fx:id="label31"/> <label fx:id="label32"/> <label fx:id="label33"/> <label fx:id="label34"/> <label fx:id="label35"/> <label fx:id="label36"/> <label fx:id="label37"/> <label fx:id="label38"/> <label fx:id="label39"/> <label fx:id="label40"/> <label fx:id="label41"/> <label fx:id="label42"/>
and here error:
exception in application start method java.lang.reflect.invocationtargetexception @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:497) @ com.sun.javafx.application.launcherimpl.launchapplicationwithargs(launcherimpl.java:389) @ com.sun.javafx.application.launcherimpl.launchapplication(launcherimpl.java:328) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:497) @ sun.launcher.launcherhelper$fxhelper.main(launcherhelper.java:767) caused by: java.lang.runtimeexception: exception in application start method @ com.sun.javafx.application.launcherimpl.launchapplication1(launcherimpl.java:917) @ com.sun.javafx.application.launcherimpl.lambda$launchapplication$152(launcherimpl.java:182) @ com.sun.javafx.application.launcherimpl$$lambda$50/1343441044.run(unknown source) @ java.lang.thread.run(thread.java:745) caused by: javafx.fxml.loadexception: file:/c:/users/brent/documents/netbeansprojects/riskfxml/dist/run1282679996/riskfxml.jar!/riskfxml/fxml.fxml:34 @ javafx.fxml.fxmlloader.constructloadexception(fxmlloader.java:2605) @ javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:2547) @ javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:2445) @ javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:3218) @ javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:3179) @ javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:3152) @ javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:3128) @ javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:3108) @ javafx.fxml.fxmlloader.load(fxmlloader.java:3101) @ riskfxml.riskfxml.start(riskfxml.java:22) @ com.sun.javafx.application.launcherimpl.lambda$launchapplication1$159(launcherimpl.java:863) @ com.sun.javafx.application.launcherimpl$$lambda$53/1527242123.run(unknown source) @ com.sun.javafx.application.platformimpl.lambda$runandwait$172(platformimpl.java:326) @ com.sun.javafx.application.platformimpl$$lambda$45/355629945.run(unknown source) @ com.sun.javafx.application.platformimpl.lambda$null$170(platformimpl.java:295) @ com.sun.javafx.application.platformimpl$$lambda$48/1753953479.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ com.sun.javafx.application.platformimpl.lambda$runlater$171(platformimpl.java:294) @ com.sun.javafx.application.platformimpl$$lambda$47/1915503092.run(unknown source) @ com.sun.glass.ui.invokelaterdispatcher$future.run(invokelaterdispatcher.java:95) @ com.sun.glass.ui.win.winapplication._runloop(native method) @ com.sun.glass.ui.win.winapplication.lambda$null$145(winapplication.java:101) @ com.sun.glass.ui.win.winapplication$$lambda$36/1963387170.run(unknown source) ... 1 more caused by: javax.xml.stream.xmlstreamexception: parseerror @ [row,col]:[34,38] message: element type "label" must followed either attribute specifications, ">" or "/>". @ com.sun.org.apache.xerces.internal.impl.xmlstreamreaderimpl.next(xmlstreamreaderimpl.java:601) @ javax.xml.stream.util.streamreaderdelegate.next(streamreaderdelegate.java:88) @ javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:2517) ... 22 more exception running application riskfxml.riskfxml java result: 1
update
ok removed '= new label()'s got rid of error, cause problem defining them in both fxml , controller trying add duplicate elements. problem how add labels gridpane in fxml in same positions when doing in loop @ bottom of sample controller code? way add them arraylist in fxml , add that? i'm aware can do
<label fx:id="label1" onmouseclicked="#labelaction" gridpane.columnindex="0" gridpane.rowindex="0" />
for each of labels there has better way hand type , place every single label right? need in same column in increasing rows.
Comments
Post a Comment