如何导入 .csv 或 .txt 文件以显示在使用 Scenebuilder 和 JavaFX 创建的高分菜单的表格视图上

How to import .csv or .txt files to be displayed on tableview of High Score Menu created with Scenebuilder and JavaFX

我正在尝试为我一直维护的项目设计高分菜单,我希望能够使用 .txt 或 .csv 文件来存储和检索所有最高分项目显示在我用 Scenebuilder 构建的高分菜单上,但我在这样做时遇到了麻烦。到目前为止,我已经尝试使用从文件中读取的传统方法,但我似乎无法将其输入到 scenebuilder 的文本区域中。任何帮助表示赞赏。谢谢

Highscore.fxml:

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

<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller.HighscoreController">
   <children>
      <VBox prefHeight="400.0" prefWidth="601.0">
         <children>
            <StackPane prefHeight="77.0" prefWidth="601.0" style="-fx-background-color: #242322;">
               <children>
                  <Label text="Highscores" textFill="#045a61">
                     <font>
                        <Font name="Wingdings" size="62.0" />
                     </font>
                  </Label>
               </children>
            </StackPane>
            <StackPane layoutX="10.0" layoutY="10.0" prefHeight="327.0" prefWidth="601.0">
               <children>
                  <TableView fx:id="table">
                    <columns>
                      <TableColumn fx:id="numColumn" prefWidth="322.6665954589844" text="No." />
                      <TableColumn fx:id="nameColumn" prefWidth="201.33331298828125" text="Name" />
                        <TableColumn fx:id="scoreColumn" prefWidth="81.3333740234375" text="Score" />
                    </columns>
                     <columnResizePolicy>
                        <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                     </columnResizePolicy>
                  </TableView>
               </children>
            </StackPane>
         </children>
      </VBox>
   </children>
</AnchorPane>

Highscore.java

package Model;

import java.io.File;
import java.util.Scanner;

public class Highscore {

    int numColumn, ScoreColumn;
    String nameColumn;

    public int getNumColumn() {
        return numColumn;
    }

    public void setNumColumn(int numColumn) {
        this.numColumn = numColumn;
    }

    public int getScoreColumn() {
        return ScoreColumn;
    }

    public void setScoreColumn(int scoreColumn) {
        ScoreColumn = scoreColumn;
    }

    public String getNameColumn() {
        return nameColumn;
    }

    public void setNameColumn(String nameColumn) {
        this.nameColumn = nameColumn;
    }

    public Highscore(int numColumn, int ScoreColumn, String nameColumn){
        this.numColumn = numColumn;
        this.ScoreColumn = ScoreColumn;
        this.nameColumn = nameColumn;
    }




}

HighscoreController.java:

package Controller;

import Model.Highscore;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Scanner;

public class HighscoreController implements Initializable {

    @FXML
    private TableView<Highscore> table;

    @FXML
    private TableColumn<Highscore, Integer> numColumn;

    @FXML
    private TableColumn<Highscore, String> nameColumn;

    @FXML
    private TableColumn<Highscore, Integer> scoreColumn;

    String fileName = "src/main/resources/Misc/Highscore.csv";
    File file = new File(fileName);



    {
        try {
            Scanner inputStream = new Scanner(file);
            while ((inputStream.hasNext())){
                String data = inputStream.next();
                Object[] values = data.split("\n");
                Object[] values_line = data.split(",");
//                Name[0] = (String)values[0];
//                Score[0] = (Integer)values[1];
//                Num[0] = (Integer)values[2];
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }


    ObservableList<Highscore> list = FXCollections.observableArrayList(
            new Highscore(1, 200, "Bob")
    );

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        numColumn.setCellValueFactory(new PropertyValueFactory<Highscore, Integer>("numColumn"));
        nameColumn.setCellValueFactory(new PropertyValueFactory<Highscore, String>("nameColumn"));
        scoreColumn.setCellValueFactory(new PropertyValueFactory<Highscore, Integer>("scoreColumn"));

        table.setItems(list);
    }
}

你需要把它放在 list 中,列表是你的项目来源,它 ObservableList 所以列表的变化会触发视图的变化,绑定完成: table.setItems(list);

ObservableList<Highscore> list = FXCollections.observableArrayList(
            new Highscore(1, 200, "Bob")
    );

    {
        try {
            Scanner inputStream = new Scanner(file);
            while ((inputStream.hasNext())){
                String data = inputStream.next();
                Object[] values = data.split("\n");
                Object[] values_line = data.split(",");
//                Name[0] = (String)values[0];
//                Score[0] = (Integer)values[1];
//                Num[0] = (Integer)values[2];
                list.add(new Highscore(Integer.valueOf(values_line[0].toString()),Integer.valueOf(values_line[1].toString()),values_line[2].toString()));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

和清洁工:

//list of Highscore items
    ObservableList<Highscore> list = FXCollections.observableArrayList();

    public void initList(){
        try {
            Scanner inputStream = new Scanner(file);
            while ((inputStream.hasNext())){
                String data = inputStream.next();
                String[] values_line = data.split(",");
//                Name[0] = (String)values[0];
//                Score[0] = (Integer)values[1];
//                Num[0] = (Integer)values[2];
                list.add(new Highscore(Integer.valueOf(values_line[0]),Integer.valueOf(values_line[1]),values_line[2]));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        //fill the list with values
        initList();
        numColumn.setCellValueFactory(new PropertyValueFactory<Highscore, Integer>("numColumn"));
        nameColumn.setCellValueFactory(new PropertyValueFactory<Highscore, String>("nameColumn"));
        scoreColumn.setCellValueFactory(new PropertyValueFactory<Highscore, Integer>("scoreColumn"));
        //bind list into the table
        table.setItems(list);
    }
    

您可以使用 stream API 加载文件并创建 HighScore 对象列表。然后你可以创建一个 ObservableList 这些 HighScore 对象。

java.util.List<HighScore> list = java.nio.file.Files.lines(Paths.get("src/main/resources/Misc/Highscore.csv"))
                                                    .map(line -> {
                                                        String[] values = line.split(",");
                                                        return new HighScore(Integer.parseInt(values[2]),
                                                                             Integer.parseInt(values[1]),
                                                                             values[0]);
                                                    })
                                                    .collect(java.util.stream.Collectors.toList());
table.setItems(javafx.collections.FXCollections.observableArrayList(list));