Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaFX-ListView不从fxml填充数据_Java_Listview_Javafx_Javafx 8_Fxml - Fatal编程技术网

JavaFX-ListView不从fxml填充数据

JavaFX-ListView不从fxml填充数据,java,listview,javafx,javafx-8,fxml,Java,Listview,Javafx,Javafx 8,Fxml,我试图用fxml填充列表视图,但在运行我的程序时,setCellFactoryfor方法不知何故没有被我的ListView调用。 是否有人可以帮助我,并解释如何修复我的代码 ListViewController.java--> setCellFactory被调用,但您使用的单元格不以任何方式显示内容: 创建数据的实例不会加载fxml,并且在未加载fxml的情况下,getBox()始终返回null 顺便说一句:重用数据以避免重新加载fxml是个好主意 通常在这种情况下,使用从fxml加载的内容创

我试图用fxml填充列表视图,但在运行我的程序时,
setCellFactory
for方法不知何故没有被我的
ListView
调用。 是否有人可以帮助我,并解释如何修复我的代码

ListViewController.java-->


setCellFactory
被调用,但您使用的单元格不以任何方式显示内容:

创建
数据的实例
不会加载fxml,并且在未加载fxml的情况下,
getBox()
始终返回
null

顺便说一句:重用
数据
以避免重新加载fxml是个好主意


通常在这种情况下,使用从fxml加载的内容创建自定义
节点
类型:

public class Data extends HBox {

    public Data() {
        // load fxml here
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/Tracker/UI/listCellItem.fxml"));
        loader.setController(this);
        loader.setRoot(this);
        try {
            loader.load();
        } catch (IOException ex) {
            throw new IllegalStateException(ex);
        }
    }

    @FXML
    private ImageView imageListCell;

    @FXML
    private Label labelListCell;

    public void setInfo(String string) {
        labelListCell.setText(string);
    }

}
listCellItem.fxml


请注意,您还应该复制旧fxml根元素的所有属性,但对于
fx:controller
属性除外

public class ListViewCell extends ListCell<String> {

    private final Data data;

    public ListViewCell() {
        this.data = new Data();
        setGraphic(this.data);
    }

    @Override
    public void updateItem(String string, boolean empty) {
        super.updateItem(string, empty);

        data.setInfo(string == null ? "AAAA" : string);
    }
}

此外,与其使用
@SuppressWarnings(“rawtypes”)
@SuppressWarnings(“unchecked”)
,为什么不简单地添加
作为类型参数??

您可以直接在列表视图中添加对象

在下面的示例中,我在fxml中创建了一个列表项,并使用for循环将该项添加到列表视图中

参见源代码示例

ListViewExample FXML代码

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

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

<AnchorPane id="AnchorPane" prefHeight="389.0" prefWidth="471.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="customlistview.ListViewExampleController">
  <children>
      <ListView fx:id="listView" layoutX="14.0" layoutY="14.0" prefHeight="389.0" prefWidth="517.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0" />
  </children>
</AnchorPane>
package customlistview;

    import java.io.IOException;
    import java.net.URL;
    import java.util.ResourceBundle;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.fxml.Initializable;
    import javafx.scene.control.ListCell;
    import javafx.scene.control.ListView;
    import javafx.util.Callback;
    import javafx.scene.layout.Pane;

    /**
     *
     * @author hemant-pc
     */
    public class ListViewExample implements Initializable {

        @FXML
        private ListView listView;

        @FXML
        Pane pane;

        public ListViewExample() {
        }

        @Override
        public void initialize(URL url, ResourceBundle rb) {
            try {
                ObservableList<Pane> list = FXCollections.observableArrayList();
                for (int i = 1; i <= 10; i++) {
                    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("ListItem.fxml"));
                    Pane listItem = fxmlLoader.load();
                    ListItemController controller = fxmlLoader.getController();
                    controller.setText("List Item " + i);
                    list.add(listItem);
                }
                listView.getItems().addAll(list);
            } catch (IOException ex) {
                Logger.getLogger(ListViewExample.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

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

    <?import javafx.scene.control.Button?>
    <?import javafx.scene.control.Label?>
    <?import javafx.scene.layout.Pane?>

    <Pane fx:id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="65.0" prefWidth="317.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="customlistview.ListItemController">
       <children>
          <Label fx:id="label" layoutX="35.0" layoutY="6.0" prefHeight="50.0" prefWidth="117.0" text="ListItem1" />
          <Button fx:id="button" layoutX="201.0" layoutY="19.0" mnemonicParsing="false" onAction="#ButtonClick" text="Button" />
       </children>
    </Pane>
 package customlistview;

    import java.net.URL;
    import java.util.ResourceBundle;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.control.Alert;
    import javafx.scene.control.Label;

    /**
     * FXML Controller class
     *
     * @author hemant-pc
     */
    public class ListItemController implements Initializable {

        @FXML
        Label label;

        /**
         * Initializes the controller class.
         */
        @Override
        public void initialize(URL url, ResourceBundle rb) {
            // TODO
        }

        @FXML
        public void ButtonClick(ActionEvent event) {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setContentText(label.getText());
            alert.showAndWait();
        }

        public void setText(String string) {
            label.setText(string);
        }

        public String getText() {
            return label.getText();
        }

    }
快照


谢谢,我试着按照你说的做了所有的事情,但还是没有发生任何事情。@DanaS你使用了
ListViewController
和包含
ListView
的fxml吗?这是我看到的唯一一个仍然可能出错的地方。如果这不是问题所在,请将断点或
println
添加到
initialize
方法和
updateItem
方法中,以检查哪些方法被调用,哪些不被调用。。。
<?xml version="1.0" encoding="UTF-8"?>

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

<AnchorPane id="AnchorPane" prefHeight="389.0" prefWidth="471.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="customlistview.ListViewExampleController">
  <children>
      <ListView fx:id="listView" layoutX="14.0" layoutY="14.0" prefHeight="389.0" prefWidth="517.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0" />
  </children>
</AnchorPane>
package customlistview;

    import java.io.IOException;
    import java.net.URL;
    import java.util.ResourceBundle;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.fxml.Initializable;
    import javafx.scene.control.ListCell;
    import javafx.scene.control.ListView;
    import javafx.util.Callback;
    import javafx.scene.layout.Pane;

    /**
     *
     * @author hemant-pc
     */
    public class ListViewExample implements Initializable {

        @FXML
        private ListView listView;

        @FXML
        Pane pane;

        public ListViewExample() {
        }

        @Override
        public void initialize(URL url, ResourceBundle rb) {
            try {
                ObservableList<Pane> list = FXCollections.observableArrayList();
                for (int i = 1; i <= 10; i++) {
                    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("ListItem.fxml"));
                    Pane listItem = fxmlLoader.load();
                    ListItemController controller = fxmlLoader.getController();
                    controller.setText("List Item " + i);
                    list.add(listItem);
                }
                listView.getItems().addAll(list);
            } catch (IOException ex) {
                Logger.getLogger(ListViewExample.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

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

    <?import javafx.scene.control.Button?>
    <?import javafx.scene.control.Label?>
    <?import javafx.scene.layout.Pane?>

    <Pane fx:id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="65.0" prefWidth="317.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="customlistview.ListItemController">
       <children>
          <Label fx:id="label" layoutX="35.0" layoutY="6.0" prefHeight="50.0" prefWidth="117.0" text="ListItem1" />
          <Button fx:id="button" layoutX="201.0" layoutY="19.0" mnemonicParsing="false" onAction="#ButtonClick" text="Button" />
       </children>
    </Pane>
 package customlistview;

    import java.net.URL;
    import java.util.ResourceBundle;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.control.Alert;
    import javafx.scene.control.Label;

    /**
     * FXML Controller class
     *
     * @author hemant-pc
     */
    public class ListItemController implements Initializable {

        @FXML
        Label label;

        /**
         * Initializes the controller class.
         */
        @Override
        public void initialize(URL url, ResourceBundle rb) {
            // TODO
        }

        @FXML
        public void ButtonClick(ActionEvent event) {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setContentText(label.getText());
            alert.showAndWait();
        }

        public void setText(String string) {
            label.setText(string);
        }

        public String getText() {
            return label.getText();
        }

    }