Laugh7 February 2016

JavaFX FXML Controller initialize method not invoked

I am trying to fill ListView from the content of a simple ArrayList. Here is 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 is my 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 />
      <        

Answers


jewelsea February 2016

The issue was that the Controller's wasn't being initialized as the asker of the question expected.

The operation of the FXMLLoader in Java 8 is a little weird.

  1. If you define a no-parameter initialize() method in your Controller and don't implement the Initializable interface, then the FXML loader will still automatically invoke the initialize method.

  2. If instead, you define a parameterized public void initialize(URL url, ResourceBundle rb) method in your Controller, then the FXML loader will not automatically invoke the initialize method unless your controller also implements the Initializable interface.

As the code in the question was using a parameterized initialize method and not also implementing Initializable, the initialization was not occurring.


Zach J February 2016

If I see correctly, you actually don't use the URL and the ResourceBundle parameters in you initialize() method. If the URL is the path to your fxml file, and the ResourceBundle contains internatonalization properties for your GUI, then you should define your initialize() method without parameters, but annotated with @FXML. The URL and the ResourceBundle should be passed to the FXMLLoader.

Like

FXMLLoader loader = new FXMLLoader(URL, rb);

and

@FXML
private void initialize() { ... }

This way initialize() will be automatically invoked.


Laugh7 February 2016

I needed to implement Initializable to the class and add an @Override before calling the method.

Post Status

Asked in February 2016
Viewed 2,770 times
Voted 7
Answered 3 times

Search




Leave an answer