java - JavaFX FXML Controller initialize method not invoked -
i trying fill listview content of simple arraylist. here my
controller.java file:
package design; import java.net.url; import java.util.arraylist; import java.util.list; import java.util.resourcebundle; import javafx.beans.property.listproperty; import javafx.beans.property.simplelistproperty; import javafx.collections.fxcollections; import javafx.event.actionevent; import javafx.fxml.fxml; import javafx.scene.control.button; import javafx.scene.control.listview; public class controller { @fxml private button buttontest; @fxml private listview<song> listviewofsongs; protected list<song> songlist = new arraylist<>(); protected listproperty<song> listproperty = new simplelistproperty<song>(); song language = new song("peter", "myalbum", "yes", 2010); public void addsong(song song){ songlist.add(song); } public void initialize(url url, resourcebundle rb) { addsong(language); listviewofsongs.setitems(fxcollections.observablelist(songlist)); } } and here style.fxml file:
<?xml version="1.0" encoding="utf-8"?> <?import javafx.geometry.insets?> <?import javafx.scene.control.button?> <?import javafx.scene.control.label?> <?import javafx.scene.control.listview?> <?import javafx.scene.control.separator?> <?import javafx.scene.control.textfield?> <?import javafx.scene.layout.columnconstraints?> <?import javafx.scene.layout.gridpane?> <?import javafx.scene.layout.pane?> <?import javafx.scene.layout.rowconstraints?> <?import javafx.scene.text.font?> <gridpane prefheight="499.0" prefwidth="700.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="design.controller"> <columnconstraints> <columnconstraints /> <columnconstraints /> <columnconstraints maxwidth="0.0" minwidth="0.0" prefwidth="0.0" /> <columnconstraints maxwidth="0.0" minwidth="0.0" prefwidth="0.0" /> </columnconstraints> <rowconstraints> <rowconstraints /> <rowconstraints /> <rowconstraints maxheight="0.0" minheight="0.0" prefheight="0.0" /> <rowconstraints maxheight="10.0" minheight="0.0" prefheight="0.0" /> <rowconstraints maxheight="10.0" minheight="0.0" prefheight="0.0" /> <rowconstraints fillheight="false" maxheight="500.0" minheight="10.0" prefheight="240.0" /> <rowconstraints fillheight="false" maxheight="500.0" minheight="10.0" prefheight="149.0" /> <rowconstraints fillheight="false" maxheight="500.0" minheight="10.0" prefheight="50.0" /> </rowconstraints> <children> <pane prefheight="64.0" prefwidth="700.0"> <children> <label layoutx="259.0" layouty="14.0" text="song library"> <font> <font name="consolas bold" size="26.0" /> </font> <padding> <insets bottom="5.0" left="5.0" right="5.0" top="5.0" /> </padding> </label> </children> </pane> <separator prefwidth="200.0" gridpane.rowindex="4" /> <listview fx:id="listviewofsongs" editable="true" prefheight="239.0" prefwidth="700.0" gridpane.rowindex="5" /> <label text="song info" gridpane.halignment="center" gridpane.rowindex="6" gridpane.valignment="top"> <gridpane.margin> <insets bottom="10.0" /> </gridpane.margin> <font> <font name="consolas" size="22.0" /> </font> <padding> <insets bottom="5.0" left="5.0" right="5.0" top="5.0" /> </padding> </label> <label gridpane.rowindex="6" /> <label text="name" gridpane.rowindex="6"> <font> <font name="calibri" size="19.0" /> </font> <gridpane.margin> <insets bottom="60.0" left="200.0" /> </gridpane.margin> </label> <textfield maxheight="-infinity" maxwidth="-infinity" minheight="-infinity" minwidth="-infinity" prefheight="25.0" prefwidth="343.0" gridpane.rowindex="6"> <gridpane.margin> <insets bottom="60.0" left="275.0" /> </gridpane.margin> </textfield> <label text="artist" gridpane.rowindex="6"> <font> <font name="calibri" size="19.0" /> </font> <gridpane.margin> <insets left="200.0" /> </gridpane.margin> </label> <textfield maxheight="-infinity" maxwidth="-infinity" minheight="-infinity" minwidth="-infinity" prefheight="25.0" prefwidth="343.0" gridpane.rowindex="6"> <gridpane.margin> <insets left="275.0" /> </gridpane.margin> </textfield> <label text="album" gridpane.rowindex="6"> <font> <font name="calibri" size="19.0" /> </font> <gridpane.margin> <insets bottom="-60.0" left="200.0" /> </gridpane.margin> </label> <textfield maxheight="-infinity" maxwidth="-infinity" minheight="-infinity" minwidth="-infinity" prefheight="25.0" prefwidth="343.0" gridpane.rowindex="6"> <gridpane.margin> <insets bottom="-60.0" left="275.0" /> </gridpane.margin> </textfield> <label text="year" gridpane.rowindex="6"> <font> <font name="calibri" size="19.0" /> </font> <gridpane.margin> <insets bottom="-120.0" left="200.0" /> </gridpane.margin> </label> <textfield maxheight="-infinity" maxwidth="-infinity" minheight="-infinity" minwidth="-infinity" prefheight="25.0" prefwidth="343.0" gridpane.rowindex="6"> <gridpane.margin> <insets bottom="-120.0" left="275.0" /> </gridpane.margin> </textfield> <button mnemonicparsing="false" prefheight="28.0" prefwidth="75.0" text="add" gridpane.rowindex="7"> <gridpane.margin> <insets left="125.0" /> </gridpane.margin> <font> <font name="corbel" size="15.0" /> </font> </button> <button mnemonicparsing="false" prefheight="28.0" prefwidth="75.0" text="edit" gridpane.rowindex="7"> <font> <font name="corbel" size="15.0" /> </font> <gridpane.margin> <insets left="325.0" /> </gridpane.margin> </button> <button mnemonicparsing="false" prefheight="28.0" prefwidth="75.0" text="delete" gridpane.rowindex="7"> <font> <font name="corbel" size="15.0" /> </font> <gridpane.margin> <insets left="525.0" /> </gridpane.margin> </button> <separator orientation="vertical" prefheight="200.0" gridpane.rowindex="7"> <gridpane.margin> <insets left="250.0" /> </gridpane.margin> <padding> <insets bottom="5.0" left="5.0" right="5.0" top="5.0" /> </padding> </separator> </children> </gridpane> the program runs, song have entered not appear in listview (and song class have tostring method).
what can do?
the issue controller's wasn't being initialized asker of question expected.
the operation of fxmlloader in java 8 little weird.
if define no-parameter
initialize()method in controller , don't implementinitializableinterface, fxml loader still automatically invoke initialize method.if instead, define parameterized
public void initialize(url url, resourcebundle rb)method in controller, fxml loader not automatically invoke initialize method unless controller implementsinitializableinterface.
as code in question using parameterized initialize method , not implementing initializable, initialization not occurring.
Comments
Post a Comment