java - Array of Button in JavaFX using Scene Builder -
lets have 2 buttons in fxml instance:
<button fx:id="button1" onaction="#onclick1" prefheight="134.0" prefwidth="134.0"></button> <button fx:id="button2" onaction="#onclick2" prefheight="134.0" prefwidth="134.0"></button>
and want have array of buttons in controller class. how can go , that? have tried:
public button button1, button2; public button[] arraybuttons = {button1, button2}
and tried making method:
public class controller { public button button1, button2; public button[] arraybuttons; public void initializebuttonarray() { arraybuttons = new button[2]; arraybuttons[1] = button1; arraybuttons[2] = button2; } }
none of these work gives me runtime exception when try array (ie. arraybutton[1].settext("test")):
java.lang.runtimeexception: java.lang.reflect.invocationtargetexception
how can go , have array of button elements fx:id?
here are:
1)@fxml annotation used cause objects private.in case make them public there no need use @fxml
2)as fxmlloader has loaded controller initialize
method called.inside sure every object linked fxml scene graph has been initialized.
🏁code(obviously 1 way , can in various other ways):
public class controller { @fxml private button button1; @fxml private button button2; public button[] arraybuttons; @fxml public void initialize(){ initializebuttonarray(); } public void initializebuttonarray() { arraybuttons = new button[2]; arraybuttons[1] = button1; arraybuttons[2] = button2; } }
Comments
Post a Comment