JFX表格视图从CSV填充数据,但在表格中不可见

时间:2019-01-04 05:46:10

标签: java javafx

我正在尝试从CSV文件填充我的JFX表格视图。

我已经使用场景生成器来设计视图。

控制器类-

public class FXMLDocumentController implements Initializable {

private Label label;
@FXML
private Button readCSVButton;
@FXML
private Button uploadDataButton;
@FXML
private Button cancelButton;
@FXML
private Button authenticateUserButton;
@FXML
private TextField userName;
@FXML
private PasswordField password;
@FXML 
private TableView<Record> tableView = new TableView<>();
@FXML 
private TableColumn <Record,String> col_ID;
@FXML 
private TableColumn <Record,String> col_barcode;
@FXML 
private TableColumn <Record,String> col_fileToDelete;
@FXML 
private TableColumn <Record,String> col_fileToUpload;
@FXML 
private TableColumn <Record,String> col_docType;
@FXML 
private TableColumn <Record,String> col_date;
@FXML 
private TableColumn <Record,String> col_requestBY;
@FXML 
private TableColumn <Record,String> col_status;

private void handleButtonAction(ActionEvent event) {
    System.out.println("You clicked me!");
    label.setText("Hello World!");
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}

@FXML
private void readCSV(ActionEvent event) {

    UploadUtil uploadtUtil= new UploadUtil();
    FileChooser fileChooser = new FileChooser();
    File selectedFile=fileChooser.showOpenDialog(password.getScene().getWindow());

     col_ID.setCellValueFactory(new PropertyValueFactory<Record,String>("id"));
     col_barcode.setCellValueFactory(new PropertyValueFactory<Record,String>("barcode"));
     col_date.setCellValueFactory(new PropertyValueFactory<Record,String>("fileToDelete"));
     col_docType.setCellValueFactory(new PropertyValueFactory<Record,String>("fileToUpload"));
     col_fileToDelete.setCellValueFactory(new PropertyValueFactory<Record,String>("doctype"));
     col_fileToUpload.setCellValueFactory(new PropertyValueFactory<Record,String>("date"));
     col_requestBY.setCellValueFactory(new PropertyValueFactory<Record,String>("requestedBy"));
     col_status.setCellValueFactory(new PropertyValueFactory<Record,String>("status"));
     tableView.getItems().setAll(uploadtUtil.readCSV(selectedFile.getAbsolutePath())); 

}

@FXML
private void uploadData(ActionEvent event) {
}

@FXML
private void cancel(ActionEvent event) {
}

@FXML
private void authenticateUser(ActionEvent event) {
    if (userName.getText().equalsIgnoreCase("naveen") && password.getText().equalsIgnoreCase("1234")) {
        readCSVButton.disableProperty().set(false);
        userName.disableProperty().set(true);
        password.disableProperty().set(true);
    }
}

}

FXML文件-

    <AnchorPane id="AnchorPane" fx:id="anchorPane" prefHeight="685.0" prefWidth="1091.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.dfg.ui.FXMLDocumentController">
   <children>
      <SplitPane dividerPositions="0.2783661119515885" layoutX="14.0" layoutY="14.0" orientation="VERTICAL" prefHeight="663.0" prefWidth="1064.0">
        <items>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="96.0" prefWidth="422.0">
               <children>
                  <Label layoutX="51.0" layoutY="42.0" prefHeight="17.0" prefWidth="152.0" text="User Name :" />
                  <Label layoutX="51.0" layoutY="82.0" prefHeight="17.0" prefWidth="152.0" text="Password   :" />
                  <Button fx:id="readCSVButton" disable="true" layoutX="581.0" layoutY="135.0" mnemonicParsing="false" onAction="#readCSV" prefHeight="32.0" prefWidth="149.0" text="Read Data from CSV" />
                  <Button fx:id="uploadDataButton" disable="true" layoutX="740.0" layoutY="134.0" mnemonicParsing="false" onAction="#uploadData" prefHeight="32.0" prefWidth="149.0" text="Upload Data to Savvion" />
                  <Button fx:id="cancelButton" cancelButton="true" layoutX="900.0" layoutY="134.0" mnemonicParsing="false" onAction="#cancel" prefHeight="32.0" prefWidth="149.0" text="Cancel" />
                  <TextField fx:id="userName" layoutX="136.0" layoutY="39.0" prefHeight="26.0" prefWidth="188.0" />
                  <PasswordField fx:id="password" layoutX="135.0" layoutY="78.0" prefHeight="26.0" prefWidth="188.0" />
                  <Button fx:id="authenticateUserButton" layoutX="422.0" layoutY="135.0" mnemonicParsing="false" onAction="#authenticateUser" prefHeight="32.0" prefWidth="149.0" text="Authenticate User" />
               </children>
            </AnchorPane>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
               <children>
                  <TabPane prefHeight="473.0" prefWidth="1062.0" tabClosingPolicy="UNAVAILABLE">
                    <tabs>
                      <Tab text="Data">
                        <content>
                          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                                 <children>
                                    <TableView fx:id="dataTable" prefHeight="415.0" prefWidth="1062.0">
                                      <columns>
                                        <TableColumn fx:id="col_ID" prefWidth="75.0" text="ID" />
                                        <TableColumn fx:id="col_barcode" prefWidth="75.0" text="Barcode" />
                                          <TableColumn fx:id="col_fileToDelete" prefWidth="150.0" text="File to Delete" />                                       
                                          <TableColumn fx:id="col_fileToUpload" prefWidth="196.0" text="File to Upload" />
                                          <TableColumn fx:id="col_docType" minWidth="0.0" prefWidth="104.0" text="Doc Type" />
                                          <TableColumn fx:id="col_date" minWidth="0.0" prefWidth="118.0" text="Date" />
                                          <TableColumn fx:id="col_requestBY" minWidth="0.0" prefWidth="191.0" text="Requested by" />
                                          <TableColumn fx:id="col_status" prefWidth="152.0" text="Status" />
                                      </columns>
                                    </TableView>
                                 </children>
                              </AnchorPane>
                        </content>
                      </Tab>
                      <Tab text="Logs">
                        <content>
                          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
                        </content>
                      </Tab>
                    </tabs>
                  </TabPane>
               </children>
            </AnchorPane>
        </items>
      </SplitPane>
   </children>
</AnchorPane>

记录班级-

  public class Record {


            public Record(String id, String barcode, String fileToDelete, String fileToUpload, String doctype, String date, String requestedBy, String status) {
            this.id = id;
            this.barcode = barcode;
            this.fileToDelete = fileToDelete;
            this.fileToUpload = fileToUpload;
            this.doctype = doctype;
            this.date = date;
            this.requestedBy = requestedBy;
            this.status = status;
        } private String  id;
            private String barcode;
            private String fileToDelete;
            private String fileToUpload;
            private String doctype;
            private String date;
            private String requestedBy;
            private String status;


            public void setId(String id) {
                this.id = id;
            }

            public void setBarcode(String barcode) {
                this.barcode = barcode;
            }

            public void setFileToDelete(String fileToDelete) {
                this.fileToDelete = fileToDelete;
            }

            public void setFileToUpload(String fileToUpload) {
                this.fileToUpload = fileToUpload;
            }

            public void setDoctype(String doctype) {
                this.doctype = doctype;
            }

            public void setDate(String date) {
                this.date = date;
            }

            public void setRequestedBy(String requestedBy) {
                this.requestedBy = requestedBy;
            }

            public void setStatus(String status) {
                this.status = status;
            }

            public String getId() {
                return id;
            }

            public String getBarcode() {
                return barcode;
            }

            public String getFileToDelete() {
                return fileToDelete;
            }

            public String getFileToUpload() {
                return fileToUpload;
            }

            public String getDoctype() {
                return doctype;
            }

            public String getDate() {
                return date;
            }

            public String getRequestedBy() {
                return requestedBy;
            }

            public String getStatus() {
                return status;
            }

    }

助手类-

 public class UploadUtil {

        public ObservableList<Record> readCSV(String filePath) {

            ObservableList<Record> dataList = FXCollections.observableArrayList();
            String FieldDelimiter = ",";

            BufferedReader br;

            try {
                br = new BufferedReader(new FileReader(filePath));

                String line;
      Integer id = 0;
                while ((line = br.readLine()) != null) {
                    if (id > 0) {
                        String[] fields = line.split(FieldDelimiter, -1);

                        Record record = new Record(id.toString(), fields[0], fields[1], fields[2],
                                fields[3], fields[4], fields[5], "Ready for Upload");
                        dataList.add(record);
                    }
                    id++;
                }

            } catch (FileNotFoundException ex) {

            } catch (IOException ex) {

            }
            return dataList;
        }
    }

我已经调试了代码,并且正在从CSV文件中获取值,并在表中设置了值,但未显示。

1 个答案:

答案 0 :(得分:0)

我复制粘贴了您的代码,并能够重现该问题。

接下来的修复需要具有有效的代码。

    表格视图的
  1. Id应与定义的字段相同:

    <TableView fx:id="tableView"
    
  2. 不要手动创建表视图:

       @FXML
       private TableView<Record> tableView;
    
  3. 由于某种原因,按钮被配置为禁用。我更改为启用:

    <Button fx:id="readCSVButton" disable="false"
    

解决上述问题就足够了。我嘲笑了readCSV()方法,并可以通过测试数据查看更新后的表。

我也将建议进一步的改进:

  1. 在面板初始化期间调用setCellValueFactory()一次,另请参见某些列的固定ID:

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        col_ID.setCellValueFactory(new PropertyValueFactory<>("id"));
        col_barcode.setCellValueFactory(new PropertyValueFactory<>("barcode"));
        col_date.setCellValueFactory(new PropertyValueFactory<>("date"));
        col_docType.setCellValueFactory(new PropertyValueFactory<>("docType"));
        col_fileToDelete.setCellValueFactory(new PropertyValueFactory<>("fileToDelete"));
        col_fileToUpload.setCellValueFactory(new PropertyValueFactory<>("fileToUpload"));
        col_requestBY.setCellValueFactory(new PropertyValueFactory<>("requestedBy"));
        col_status.setCellValueFactory(new PropertyValueFactory<>("status"));
    }
    
  2. readCSV()设为静态并返回List类型:

    static List<Record> readCSV(String filePath) {
    

    在这种情况下,您不应该每次都创建UploadUtil对象。 List接口足以用于tableView.getItems().setAll()方法。

  3. 为用户未选择文件的情况提供保护:

    if (selectedFile != null) { 
        tableView.getItems().setAll(
            uploadtUtil.readCSV(selectedFile.getAbsolutePath()));
    }