java - JavaFX Controller not getting UI fields initialized -


i developing javafx project via scene builder. created quite long fxml file (i reporting snippet) , associated controller. furthermore, wrote application class:

public class main extends application {     @override     public void start(stage stage) {         parent root;         try {             root = fxmlloader.load(getclass().getresource("myfxml.fxml"));         } catch (ioexception e) {             e.printstacktrace();             return;         }          scene scene = new scene(root);          stage.settitle("popolamento dati ambasciata");         stage.setscene(scene);         stage.show();     }      public static void main(string[] args) {         launch(args);     } } 

fxml:

<?xml version="1.0" encoding="utf-8"?>  <?import javafx.geometry.*?> <?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?>  <gridpane maxheight="-infinity" maxwidth="-infinity" minheight="-infinity" minwidth="-infinity" prefheight="600.0" prefwidth="1150.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.fxcontroller">     ... <children>         <button id="addobject" mnemonicparsing="false" onaction="#addobject" text="add object" gridpane.columnindex="1" gridpane.halignment="center" gridpane.rowindex="10" />         <textfield id="objectname" gridpane.columnindex="1" gridpane.columnspan="2" gridpane.rowindex="1" /> 

controller:

public class fxcontroller {  @fxml button addobject; @fxml textfield objectname;  @fxml public void addobject() {     objectname.gettext(); } } 

}

the application starts correctly , event handler invoked, when accessing textfield objectname, null. doing wrong?

you should use fx:id attribute instead of id.

if @ documentation here:

assigning fx:id value element creates variable in document's namespace can later referred variable dereference attributes

note can still use idattribute:

additionally, if object's type defines "id" property, value passed objects setid() method.

so in case:

<button fx:id="addobject" ... /> <textfield fx:id="objectname" ... /> 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -