java - JavaFX bind a controller variable to a component property -
say have controller with
@fxml private observablelist<string> mystrings = fxcollections.observablearraylist();
is possible write fxml wire listview mystrings
items?
my first try was:
<listview> <items fx:id="mystrings"/> </listview>
but complains fx:id
not valid in position. tried
<listview items="${controller.mystrings}"/>
...but couldn't resolve value.
please not post solution:
<listview fx:id="mystringslistview"/> // in controller @fxml private listview<string> mystringslistview; @fxml public void initialize() { mystringslistview.setitems(mystrings); }
this doing amount of indirection , boilerplate here hurts me.
the following works
<?xml version="1.0" encoding="utf-8"?> <?import javafx.scene.layout.borderpane?> <?import javafx.scene.control.listview?> <borderpane xmlns:fx="http://javafx.com/fxml/1" fx:controller="listviewcontroller"> <center> <listview items="${controller.mystrings}" /> </center> </borderpane>
with following controller (the main difference, think, being either didn't define accessor method list, or named incorrectly):
import javafx.collections.fxcollections; import javafx.collections.observablelist; public class listviewcontroller { private final observablelist<string> mystrings = fxcollections.observablearraylist(); public listviewcontroller() { mystrings.addall("one", "two", "three"); } public observablelist<string> getmystrings() { return mystrings ; } }
this quick test:
import javafx.application.application; import javafx.fxml.fxmlloader; import javafx.scene.scene; import javafx.stage.stage; public class listviewitemsfromcontrollertest extends application { @override public void start(stage primarystage) throws exception{ primarystage.setscene(new scene(fxmlloader.load(getclass().getresource("listviewitemsfromcontroller.fxml")))); primarystage.show(); } public static void main(string[] args) { launch(args); } }
produces
Comments
Post a Comment