0

Good day. I can not understand how to fill a TableView in javaFX.

There is a collection with a list of files:

private ObservableList<File> fileList = FXCollections.observableArrayList();

There is a created gui with a table. When I try to fill a table (file names from the list), I get NPE exception.

class Filelist

public class FileList {

    private ObservableList<File> fileList = FXCollections.observableArrayList();

    public void getDirContent(){
        fileList.addAll(File.listRoots());
    }

    public void viewDir(Path path){

            File folder = path.toFile();
            fileList.clear();
            fileList.addAll(folder.listFiles());
    }

    public ObservableList<File> getFileList() {
        return fileList;
    }
}
public class FileController {
    private FileList fileList = new FileList();
    @FXML
    private TableView <File> tableFileList;
    @FXML
    private TableColumn<File, String> columnName;
    @FXML
    private Label countLabel;

    @FXML
    private void initialize(){
        columnName.setCellValueFactory(new PropertyValueFactory<>("Name"));
        fileList.getDirContent();
        tableFileList.setItems(fileList.getFileList());
       // countLabel.setText("Total: " + fileList.getFileList().size());
    }

The line causing the NPE is tableFileList.setItems(fileList.getFileList());

fxml file:

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

<?import javafx.scene.canvas.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700.0" prefWidth="1024.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="abnod.filemanager.controller.FileController">
    <columnConstraints>
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
    </columnConstraints>
    <rowConstraints>
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    </rowConstraints>
    <children>
        <VBox prefHeight="200.0" prefWidth="100.0">
            <children>
                <ToolBar prefHeight="40.0" prefWidth="200.0">
                    <items>
                        <Button mnemonicParsing="false" text="Back" />
                        <Button mnemonicParsing="false" onAction="#showAddDialog" text="New" />
                        <Button mnemonicParsing="false" text="Copy" />
                        <Button mnemonicParsing="false" text="Move" />
                        <Button mnemonicParsing="false" text="Delete" />
                    </items>
                </ToolBar>
                <AnchorPane prefHeight="30.0" prefWidth="427.0">
                    <children>
                        <ToolBar layoutX="-48.0" layoutY="-6.0" prefHeight="40.0" prefWidth="200.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
                            <items>
                                <Button mnemonicParsing="false" prefHeight="25.0" prefWidth="64.0" text="Exit" />
                            </items>
                        </ToolBar>
                        <TextField layoutX="85.0" layoutY="2.0" AnchorPane.leftAnchor="90.0" AnchorPane.rightAnchor="70.0" />
                        <Button layoutX="450.0" layoutY="2.0" mnemonicParsing="false" text="Search" AnchorPane.rightAnchor="10.0" />
                    </children>
                </AnchorPane>
                <AnchorPane VBox.vgrow="ALWAYS">
                    <children>
                        <TableView prefHeight="597.0" prefWidth="512.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                            <columns>
                                <TableColumn fx:id="columnName" editable="false" minWidth="200.0" prefWidth="267.0" text="Files" />
                            </columns>
                            <columnResizePolicy>
                                <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                            </columnResizePolicy>
                        </TableView>
                    </children>
                </AnchorPane>
                <AnchorPane maxHeight="-Infinity" minHeight="-Infinity" prefHeight="34.0">
                    <children>
                        <Label fx:id="countLabel" layoutY="36.0" prefHeight="7.0" prefWidth="140.0" text="Total Files and Dirs" AnchorPane.bottomAnchor="-1.0" AnchorPane.leftAnchor="0.0">
                            <padding>
                                <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                            </padding>
                        </Label>
                    </children>
                </AnchorPane>
            </children>
        </VBox>
    </children>
</GridPane>
fabian
  • 80,457
  • 12
  • 86
  • 114

0 Answers0