JavaFX FXML Controller初始化方法未被调用 [英] JavaFX FXML Controller initialize method not invoked

查看:209
本文介绍了JavaFX FXML Controller初始化方法未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从简单的ArrayList的内容中填充ListView。这是我的



Controller.java文件:

 包装设计; 
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;

公共类控制器{

@FXML private Button buttontest;
@FXML private ListView< Song> listViewofSongs;

protected List< Song> songList = new ArrayList<>();
protected ListProperty< Song> listProperty = new SimpleListProperty< Song>();
Song Language =新歌(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));
}



}

这是我的Style.fxml文件:

 <?xml version =1.0encoding =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.0prefWidth =700.0xmlns =http://javafx.com/javafx/8.0.65xmlns:fx =http://javafx.com / fxml / 1fx:controller =design.Controller>
< columnConstraints>
< ColumnConstraints />
< ColumnConstraints />
< ColumnConstraints maxWidth =0.0minWidth =0.0prefWidth =0.0/>
< ColumnConstraints maxWidth =0.0minWidth =0.0prefWidth =0.0/>
< / columnConstraints>
< rowConstraints>
< RowConstraints />
< RowConstraints />
< RowConstraints maxHeight =0.0minHeight =0.0prefHeight =0.0/>
< RowConstraints maxHeight =10.0minHeight =0.0prefHeight =0.0/>
< RowConstraints maxHeight =10.0minHeight =0.0prefHeight =0.0/>
< RowConstraints fillHeight =falsemaxHeight =500.0minHeight =10.0prefHeight =240.0/>
< RowConstraints fillHeight =falsemaxHeight =500.0minHeight =10.0prefHeight =149.0/>
< RowConstraints fillHeight =falsemaxHeight =500.0minHeight =10.0prefHeight =50.0/>
< / rowConstraints>
< children>
< Pane prefHeight =64.0prefWidth =700.0>
< children>
< Label layoutX =259.0layoutY =14.0text =Song Library>
< font>
< Font name =Consolas Boldsize =26.0/>
< / font>
< padding>
< Insets bottom =5.0left =5.0right =5.0top =5.0/>
< / padding>
< / Label>
< / children>
< / Pane>
< Separator prefWidth =200.0GridPane.rowIndex =4/>
< ListView fx:id =listViewofSongseditable =trueprefHeight =239.0prefWidth =700.0GridPane.rowIndex =5/>

< Label text =Song InfoGridPane.halignment =CENTERGridPane.rowIndex =6GridPane.valignment =TOP>
< GridPane.margin>
< Insets bottom =10.0/>
< /GridPane.margin>
< font>
<字体名称=Consolassize =22.0/>
< / font>
< padding>
< Insets bottom =5.0left =5.0right =5.0top =5.0/>
< / padding>
< / Label>
< Label GridPane.rowIndex =6/>
< Label text =NAMEGridPane.rowIndex =6>
< font>
<字体名称=Calibrisize =19.0/>
< / font>
< GridPane.margin>
< Insets bottom =60.0left =200.0/>
< /GridPane.margin>
< / Label>
< TextField maxHeight = - InfinitymaxWidth = - InfinityminHeight = - InfinityminWidth = - InfinityprefHeight =25.0prefWidth =343.0GridPane.rowIndex =6>
< GridPane.margin>
< Insets bottom =60.0left =275.0/>
< /GridPane.margin>
< / TextField>
< Label text =ARTISTGridPane.rowIndex =6>
< font>
<字体名称=Calibrisize =19.0/>
< / font>
< GridPane.margin>
< Insets left =200.0/>
< /GridPane.margin>
< / Label>
< TextField maxHeight = - InfinitymaxWidth = - InfinityminHeight = - InfinityminWidth = - InfinityprefHeight =25.0prefWidth =343.0GridPane.rowIndex =6>
< GridPane.margin>
< Insets left =275.0/>
< /GridPane.margin>
< / TextField>
< Label text =ALBUMGridPane.rowIndex =6>
< font>
<字体名称=Calibrisize =19.0/>
< / font>
< GridPane.margin>
< Insets bottom = - 60.0left =200.0/>
< /GridPane.margin>
< / Label>
< TextField maxHeight = - InfinitymaxWidth = - InfinityminHeight = - InfinityminWidth = - InfinityprefHeight =25.0prefWidth =343.0GridPane.rowIndex =6>
< GridPane.margin>
< Insets bottom = - 60.0left =275.0/>
< /GridPane.margin>
< / TextField>
< Label text =YEARGridPane.rowIndex =6>
< font>
<字体名称=Calibrisize =19.0/>
< / font>
< GridPane.margin>
< Insets bottom = - 120.0left =200.0/>
< /GridPane.margin>
< / Label>
< TextField maxHeight = - InfinitymaxWidth = - InfinityminHeight = - InfinityminWidth = - InfinityprefHeight =25.0prefWidth =343.0GridPane.rowIndex =6>
< GridPane.margin>
< Insets bottom = - 120.0left =275.0/>
< /GridPane.margin>
< / TextField>
< Button mnemonicParsing =falseprefHeight =28.0prefWidth =75.0text =ADDGridPane.rowIndex =7>
< GridPane.margin>
< Insets left =125.0/>
< /GridPane.margin>
< font>
<字体名称=Corbelsize =15.0/>
< / font>
< / Button>
< Button mnemonicParsing =falseprefHeight =28.0prefWidth =75.0text =EDITGridPane.rowIndex =7>
< font>
<字体名称=Corbelsize =15.0/>
< / font>
< GridPane.margin>
< Insets left =325.0/>
< /GridPane.margin>
< / Button>
< Button mnemonicParsing =falseprefHeight =28.0prefWidth =75.0text =DELETEGridPane.rowIndex =7>
< font>
<字体名称=Corbelsize =15.0/>
< / font>
< GridPane.margin>
< Insets left =525.0/>
< /GridPane.margin>
< / Button>
< Separator orientation =VERTICALprefHeight =200.0GridPane.rowIndex =7>
< GridPane.margin>
< Insets left =250.0/>
< /GridPane.margin>
< padding>
< Insets bottom =5.0left =5.0right =5.0top =5.0/>
< / padding>
< / Separator>
< / children>
< / GridPane>

程序运行,但我输入的歌曲没有出现在ListView(和Song类)中确实有一个toString方法)。



我该怎么办?

解决方案

问题在于控制器没有被初始化为预期问题的提问者。



在Java 8中FXMLLoader的操作有点奇怪。


  1. 如果你定义Controller中的无参数 initialize()方法,并且不实现 可初始化 界面,然后FXML加载程序仍然会自动调用initialize方法。 / p>


  2. 相反,如果您在定义参数化 public void initialize(URL url,ResourceBundle rb)方法控制器,那么FXML加载器不会自动调用初始化方法,除非你的控制器也实现 可初始化 interface。


由于问题中的代码使用的是参数化初始化方法而不是实现Initializable,初始化没有发生。


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 />
      <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, but the song I have entered does not appear in the ListView (and the Song class does have a toString method).

What can I do?

解决方案

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.

这篇关于JavaFX FXML Controller初始化方法未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆