java - Can't execute button style methods (FXML button) from controller class. How can I setstyles of a FXML button and not using CSS for this? -
i can't set style of button within if-then-statement or method.
it compiles gives error when running application. idea color button when parameter "setnrcolor" equal 7. button assigned @fxml label. logic works when not use setstyle / settextfill methods referring button.
when running, following error: exception in thread "javafx application thread" java.lang.nullpointerexception. occurs when "setstyle methode being called.
a simple explanation saying not possible make sense. searches far didn't got answer though. appreciated.
public class fxmldocumentcontroller implements initializable { @fxml private label label; @fxml private button btn; @fxml private textfield text; private int nrcolor; public void setbuttoncolor (int setnrcolor){ boolean bln1; nrcolor = setnrcolor; if(nrcolor==7){ //expression of type boolean system.out.println("same number: set color button black"); //btn.setstyle("-fx-base:black;"); //this line doesnt work bln1 = true; //btn.getstyleclass().remove("armed"); } else { system.out.println("different number: set color button red"); //btn.setstyle("-fx-base:red;"); //doesnt work bln1 = false; trigger(); } system.out.println("output =" + bln1); } java.lang.nullpointerexception: @ test_issue.fxmldocumentcontroller.trigger(fxmldocumentcontroller.java:64) @ test_issue.fxmldocumentcontroller.setbuttoncolor(fxmldocumentcontroller.java:53) @ test_issue.flashinglight.lambda$start$1(flashinglight.java:25) @ com.sun.scenario.animation.shared.timelineclipcore.visitkeyframe(timelineclipcore.java:239) @ com.sun.scenario.animation.shared.timelineclipcore.playto(timelineclipcore.java:180) @ javafx.animation.timeline.impl_playto(timeline.java:176) @ javafx.animation.animationaccessorimpl.playto(animationaccessorimpl.java:39) @ com.sun.scenario.animation.shared.finiteclipenvelope.timepulse(finiteclipenvelope.java:124) @ javafx.animation.animation.impl_timepulse(animation.java:1102) @ javafx.animation.animation$1.lambda$timepulse$25(animation.java:186) @ java.security.accesscontroller.doprivileged(native method) @ javafx.animation.animation$1.timepulse(animation.java:185) @ com.sun.scenario.animation.abstractmastertimer.timepulseimpl(abstractmastertimer.java:344) @ com.sun.scenario.animation.abstractmastertimer$mainloop.run(abstractmastertimer.java:267) @ com.sun.javafx.tk.quantum.quantumtoolkit.pulse(quantumtoolkit.java:506) @ com.sun.javafx.tk.quantum.quantumtoolkit.pulse(quantumtoolkit.java:490) @ com.sun.javafx.tk.quantum.quantumtoolkit.lambda$runtoolkit$404(quantumtoolkit.java:319) @ 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$148(winapplication.java:191) @ java.lang.thread.run(thread.java:745) ///////////////////////////////////////////// <?xml version="1.0" encoding="utf-8"?> <?import java.lang.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <anchorpane id="anchorpane" prefheight="200" prefwidth="320" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test_issue.fxmldocumentcontroller"> <children> <label fx:id="label" layoutx="126" layouty="120" minheight="16" minwidth="69" /> <button fx:id="btn" layoutx="104.0" layouty="72.0" mnemonicparsing="false" prefheight="56.0" prefwidth="136.0" text="button" /> <textfield fx:id="text" layoutx="41.0" layouty="14.0" prompttext="hallo" /> </children> </anchorpane>
you shouldn't create objects of controller class, need extract if loader:
fxmlloader loader = new fxmlloader(getclass().getresource("/main.fxml")); anchorpane root = loader.load(); fxmldocumentcontroller controller = loader.getcontroller(); controller.setbuttoncolor(...);
Comments
Post a Comment