使用FXML TableView列出文件名 - 无法填充数据

时间:2016-01-24 09:24:59

标签: java javafx tableview fxml

我正在尝试使用FXML以表格格式填充文件名。

我可以显示表格,但不会显示行。

用户在运行时将选择目录名称。

screenshot of the current FXML window

Main.java

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.AnchorPane;
import javafx.fxml.FXMLLoader;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        try {
            AnchorPane root =(AnchorPane)FXMLLoader.load(getClass().getResource("Utility.fxml"));
            Scene scene = new Scene(root,600,600);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.setTitle("testing");
            primaryStage.show();
        } catch(Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

AppController.java

import java.io.File;
import java.util.Arrays;
import java.util.List;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.stage.DirectoryChooser;

public class AppController
{
    @FXML private Label BrowseStatus;
    @FXML private TextField TDpath;
    @FXML private Button CreateH2H;
    @FXML private TableView<String> FileListTable;
    @FXML private TableColumn<FilesInDir,String> FileNameCol;
    @FXML private ObservableList<String> fidlist;

    @FXML 
    protected void handleBrowseWindowsExplorer(ActionEvent event){
        DirectoryChooser TestDataDir = new DirectoryChooser();
        TestDataDir.setTitle("Select path");
        File selectedDir = TestDataDir.showDialog(null);
        if(selectedDir == null){
            BrowseStatus.setText("Nothing choosen");
            TDpath.setText("");
        } else {
            TDpath.setText(selectedDir.getAbsolutePath());
            FileListTable = new TableView<String>();
            FileNameCol.setCellValueFactory(new PropertyValueFactory<FilesIndDir,String>("FileName"));
            FileListTable.setPlaceholder(BrowseStatus);
            BrowseStatus.setText("Folder has been selected");
            File tFile = new File(TDpath.getText());
            File[] listOfFiles = tFile.listFiles();
            fidlist = FXCollections.observableArrayList();
            List<String> fileNameList = null;
            for (int i=0; i<listOfFiles.length; i++) {
                fileNameList = Arrays.asList(listOfFiles[i].getName());
            }
            fidlist.addAll(fileNamesList);
            FileListTable.setItems(fidlist);
        }
    }
}

Utility.fxml

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.collection.*?>
<?import javafx.collections.FXCollections?>
<?import javafx.geometry.*?>
<?import javafx.scene.chart.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.web.*?>
<?import javafx.scene.control.cell.PropertyValueFactory?>

<AnchorPane fx:controller="application.AppController" id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns:fx="http://javafx.com/fxml">
    <children>
        <GridPane>
            <children>
                <MenuBar maxWidth="-Infinity" prefWidth="800.0" GridPane.ColumnIndex="0" GridPane.RowIndex="0">
                    <menus>
                        <Menu mnemonicParsing="false" text="File">
                            <items>
                                <MenuItem mnemonicParsing="false" text="Close" />
                            </items>
                        </Menu>
                    </menus>
                </MenuBar>
                <BorderPane GridPane.ColumnIndex="0" GridPane.RowIndex="1">
                    <top>
                    <TabPane PrefHeight="700.0" PrefWidth="900.0" tabClosingPolicy="UNAVAILABLE">
                        <Tab text="Files tab ">
                            <content>
                                <GridPane hgap="10" vgap="10">
                                    <padding><Insets top="25" right="25" bottom="10" left="25" /></padding>
                                    <children>
                                        <Label text="Path :" GridPane.ColumnIndex="0" GridPane.RowIndex="1" />
                                        <TextField fx:id="TDpath" GridPane.ColumnIndex="0" GridPane.RowIndex="1" />
                                        <Button text="Browse" onAction="#handleBrowseWindowsExplorer" GridPane.ColumnIndex="3" GridPane.RowIndex="1" />
                                        <VBox prefHeight="300.0" prefWidth="400.0" spacing="6.0" VBox.vgrow="ALWAYS" GridPane.ColumnIndex="0" GridPane.RowIndex="3" GridPane.columnSpan="4" >
                                        <children>
                                            <TableView fx:id="FileListTable" >
                                                <placeholder><Label fx:id="BrowseStatus" text="No files in selected directory" /></placeholder>
                                                <columns>
                                                    <TableColumn fx:id="FileNameCol" text="File Names" prefWidth="400">
                                                        <cellValueFactory>
                                                            <PropertyValueFactory property="FileName" />
                                                        </cellValueFactory>
                                                    </TableColumn>
                                                </columns>
                                            </TableView>
                                        </children>
                                        </VBox>
                                    </children>
                                </GridPane>
                            </content>
                        </Tab>
                    </TabPane>
                </top>
                </BorderPane>
            </children>
        </GridPane>
    </children>
</AnchorPane>

FilesInDir.java

import javafx.beans.property.SimpleStringProperty;

public class FilesInDir {
    private final SimpleStringProperty FileName = new SimpleStringProperty();

    public FilesInDir(String fName) {
        setFileName(fName);
    }

    public String FileNameProperty() {
        return FileName.get();
    }

    public void setFileName(String fName) {
        this.Filename.set(fName);
    }
}

1 个答案:

答案 0 :(得分:1)

在这个答案中,我只是忽略了所有的拼写错误,因为似乎有一个可编译的代码版本并指出其他错误:

FilesInDir

如果我有你的意图,这个类应该包含应该显示的文件的信息。除了忽略以小写字母开始类成员标识符的命名约定外,这里的真正问题是FileNameProperty。带有后缀Property的属性方法必须返回属性本身,PropertyValueFactory依赖于该事实。它应该是这样的:

public StringProperty fileNameProperty() {
    return FileName;
}

此外,您没有将该类用作TableView

类型参数
@FXML
private TableView<FilesInDir> FileListTable;

AppController.handleBrowseWindowsExplorer

主要问题在于此方法:

  1. 没有必要让fidlist成为一个字段,更不用说用@FXML进行注释了。事实上,根本不需要创建新的ObservableList
  2. 您重新创建由TableView创建并注入的FXMLLoader,并且不会将其插入到场景图中,但您使用新的TableView,而不是一个显示在屏幕上。
  3. 如果未选择任何文件,则TextField将被清空,但之前所选目录中TableView中的项目仍然存在,从而导致行为不一致。
  4. {li> fileNameList = Arrays.asList(listOfFiles[i].getName());for循环中使用包含单个元素的新List替换列表。
  5. File tFile = new File(TDpath.getText());从通过将所选文件转换为String创建的String重新创建文件。这是不必要的;你应该只使用selectedDir
  6. 改变这样的方法应该有效,假设您修复了FilesInDir提到的点:

    @FXML
    protected void handleBrowseWindowsExplorer(ActionEvent event) {
        DirectoryChooser TestDataDir = new DirectoryChooser();
        TestDataDir.setTitle("Select path");
        File selectedDir = TestDataDir.showDialog(null);
    
        if (selectedDir == null) {
            BrowseStatus.setText("Nothing choosen");
            FileListTable.getItems().clear();
            TDpath.setText("");
        } else {
            TDpath.setText(selectedDir.getAbsolutePath());
            BrowseStatus.setText("Folder has been selected");
            File[] listOfFiles = selectedDir.listFiles();
    
            ArrayList<FilesInDir> fidlist = new ArrayList<>(listOfFiles.length);
    
            for (File listOfFile : listOfFiles) {
                fidlist.add(new FilesInDir(listOfFile.getName()));
            }
    
            FileListTable.getItems().setAll(fidlist);
        }
    }