JavaFX 填充一个简单的 listView

标签 java listview javafx scenebuilder

我在这个例子中使用场景生成器,我有一个 ListView ,我希望在运行应用程序时它已经被填充,但我似乎没有找到一种方法来做到这一点,它根本不起作用, ListView 也没有出现。

这是我的 FXMLTasker.fxml

    <?xml version="1.0" encoding="UTF-8"?>

    <?import javafx.scene.control.ListView?>
    <?import javafx.scene.control.SplitPane?>
    <?import javafx.scene.layout.AnchorPane?>

    <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="557.0" prefWidth="1012.0" style="-fx-background-color: #0288D1;" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1">
       <children>
          <SplitPane dividerPositions="0.5" layoutX="-8.0" layoutY="35.0" prefHeight="529.0" prefWidth="1027.0" style="-fx-background-color: #EEEEEE;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="28.0">
            <items>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
                   <children>
                      <ListView fx:id="list_todo" editable="true" layoutX="14.0" layoutY="14.0" prefHeight="508.0" prefWidth="485.0" />
                   </children></AnchorPane>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="527.0" prefWidth="640.0" />
            </items>
          </SplitPane>
       </children>
    </AnchorPane>

这是我的 Main.class

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.stage.Stage;

/**
 *
 * @author samuel
 */
public class JavaFXApplication3 extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLTasker.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }



    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

这是我的 Controller

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javax.swing.JOptionPane;

/**
 *
 * @author samuel
 */
public class FXMLDocumentController implements Initializable {

   @FXML
   private ListView list_todo;

   private ObservableList<String> items = FXCollections.observableArrayList();


    @Override
    public void initialize(URL url, ResourceBundle rb) {
        list_todo.setItems(items);
        items.add("First task");
        items.add("Second task");
    }    

}

问题是当我运行应用程序时,列表没有出现,它应该很简单,但由于某种原因它不起作用。谁能找出原因吗?

最佳答案

Controller 指定到您的 fxml 文件,并包含以下所有其他详细信息,

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="557.0" prefWidth="1012.0" style="-fx-background-color: #0288D1;" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="FXMLDocumentController">


</AnchorPane>

添加具有完整指定名称的 Controller 类(带有包名称,如果有的话)

关于JavaFX 填充一个简单的 listView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47509117/

相关文章:

android - 在 Android 中排序列表查看数据

android - JSON 解析成 ListView

java - JFX 将图像向上和向下缩放到父级

java - 将整数(非字符串)数据插入 JavaFX2 TableView

java - 打印字符串中唯一元音的数量,Java

java - 我们可以重新分发 Oracle tools.jar 吗?

Java-数据存储和自动运行程序?

android - 如何按位置从 ListView 获取 View 并将文本设置为项目?

java - 使用 countDownLatch 更新两个或多个 ProgressBar

java - 使用 java 的 MVC - 返回 NULL