java - Can't instantiate class fields in JavaFX project -


so i'm trying move swing javafx , declare start() , launch() methods without knowing how work. below code prints false , false console. when click button in gui, built scene builder, executes mymethod(), time prints true. why primarystage isn't instantiated?

addition information: made class controller, same reason - needs access stage reference. full version of main, didn't post, implements initializable if matters.

as bonus question wondering if need field of primarystage reference application stage, there 1 of?, in mymethod().

public class main extends application {      private stage primarystage;      public void start(stage primarystage) {         this.primarystage = primarystage;         try {             scene scene = new scene(fxmlloader.load(getclass().getresource("sample.fxml")),600,400);             primarystage.setscene(scene);         } catch(exception e) {             e.printstacktrace();         }         primarystage.show();         //both lines below print false; should.         system.out.println(this.primarystage == null);         mymethod();     }      public static void main(string[] args) {         launch(args);     }      public void mymethod() {         system.out.println(primarystage == null);     } } 

edit

placing fxml document in same folder above class let run main see button indeed print true.

<?xml version="1.0" encoding="utf-8"?>  <?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.layout.anchorpane?>  <hbox xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.main">    <children>       <button mnemonicparsing="false" onaction="#mymethod" text="button" />    </children> </hbox> 

that because fxmlloader create new instance of application.main class, in start() method not invoked , private stage primarystage null.

it better separate main class , controller fxml, , pass primary stage later if necessary:

... try {     fxmlloader loader = new fxmlloader( getclass().getresource( "sample.fxml" ) );     scene scene = new scene( loader.load(), 600, 400 );      ( (mycontroller) loader.getcontroller() ).setprimarystage(primarystage);     primarystage.setscene( scene ); } catch ( exception e ) {     e.printstacktrace(); } ... 

where mycontroller class can simple as:

public class mycontroller {      private stage primarystage;      public void setprimarystage(stage primarystage) {         this.primarystage = primarystage;     } } 

but can implement initializable interface. refer introduction fxml :: controllers.

also note can scene , stage node part of constructed scene graph (i.e. stage shown) by:

scene scene = anynode.getscene(); stage primarystage = (stage) anynode.getscene().getwindow(); 

of course, secondary stages created getwindow() return stage , not primary stage.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -