java - JavaFX fxml loader does not work properly -
testfx.java :
public class testfx extends application { @override public void start(stage primarystage) throws exception { try{ fxmlloader loader = new fxmlloader(); loader.setlocation(getclass().getresource("/testfx/view/test.fxml")); system.out.println("after set location"); //problem anchorpane root = (anchorpane)loader.load(); system.out.println("does not happen"); testfxcontroller listcontroller = loader.getcontroller(); listcontroller.start(); scene scene = new scene(root, 200, 300); scene.getstylesheets().add(getclass().getresource("application.css").toexternalform()); primarystage.setscene(scene); primarystage.show(); } catch (exception ex){ system.out.println("error"); } } public static void main(string[] args) { launch(args); } }
testfxcontroller.java :
package testfx.view; import javafx.collections.fxcollections; import javafx.collections.observablelist; import javafx.fxml.fxml; import javafx.scene.control.listview; public class testfxcontroller { @fxml listview<string> listview; private observablelist<string> obslist; public void start() { // create observablelist // arraylist obslist = fxcollections.observablearraylist("giants", "patriots", "jaguars"); listview.setitems(obslist); } }
test.fxml :
<?xml version="1.0" encoding="utf-8"?> <?import javafx.scene.layout.anchorpane?> <?import javafx.scene.control.listview?> <anchorpane xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.testfxcontroller"> <listview fx:id="listview" anchorpane.topanchor="10" anchorpane.leftanchor="10" anchorpane.rightanchor="10" anchorpane.bottomanchor="10" /> </anchorpane>
when run testfx.java, system prints:
after set location error
this professor's code , cannot seem running. realized main problem in line of code anchorpane root = (anchorpane)loader.load();
have no idea how fix this, can help?
the value fx:controller
attribute wrong (unless have different controller class 1 posted)
the controller want use: testfx.view.testfxcontroller
attribute value in fxml: view.testfxcontroller
!= testfx.view.testfxcontroller
assuming there no other error cannot reproduced information in question, fixing attribute value should work.
Comments
Post a Comment