java - How to add a button to a panel using a method and access it from the main? -


let's want create method adds button stackpane. want access button, exemple want add eventhandler main:

public class test extends application {      @override     public void start(stage primarystage) throws parseexception {                 stackpane root = new stackpane();         addbutton(root);         // here want add event handler b cannot access         // b.addeventhandler();          scene scene = new scene(root, 300, 250);          primarystage.settitle("hello world!");         primarystage.setscene(scene);         primarystage.show();     }      public void addbutton(stackpane sp){         final button b = new button("test");         sp.getchildren().add(b);     }      /**      * @param args command line arguments      */     public static void main(string[] args) {         launch(args);     }  } 

since button local addbutton need have reference it. return reference of button addbutton method , use it.

it better use way

public button addbutton(){      //create button } 

you can add button stackpane in start()

alternatively,

this approach not recommended.

you can button out of stackpane

button  button = (button)root.getchildren().get(0); 

n.b. use if sure of position of button

update

if want separate design action performed controls, javafx provides eligant way it, using fxml.

it helps design ui without indulgence of actions need perform. , later, these fxml's can binded actions through controllers(java interface)


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -