我正在尝试从简单的ArrayList的内容中填充ListView。这是我的
Controller.java文件:
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));
}
}
这是我的Style.fxml文件:
<?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>
程序运行,但我输入的歌曲没有出现在ListView中(而Song类确实有toString方法)。
我该怎么办?
答案 0 :(得分:14)
问题在于,财务主任并未作为预期问题的提问者进行初始化。
在Java 8中FXMLLoader的操作有点奇怪。
如果在Controller中定义了无参数initialize()
方法并且没有实现Initializable
接口,那么FXML加载器仍会自动调用initialize方法。 / p>
如果您在Controller中定义参数化public void initialize(URL url, ResourceBundle rb)
方法,那么FXML加载程序将不会自动调用初始化方法,除非您的控制器也实现{{3接口。
由于问题中的代码使用的是参数化初始化方法而不是实现Initializable,因此未进行初始化。
答案 1 :(得分:0)
如果我看得正确,您实际上并未使用initialize()
方法中的URL和ResourceBundle参数。
如果URL是fxml文件的路径,并且ResourceBundle包含GUI的internatonalization属性,那么您应该定义不带参数的initialize()
方法,但使用@FXML
进行注释。应将URL和ResourceBundle传递给FXMLLoader。
像
FXMLLoader loader = new FXMLLoader(URL, rb);
和
@FXML
private void initialize() { ... }
这样会自动调用initialize()
。
答案 2 :(得分:0)
我需要在类中实现Initializable并在调用方法之前添加@Override。