java—如何导入.csv或.txt文件以显示在用scenebuilder和javafx创建的高分菜单的tableview上

oxcyiej7  于 2021-07-08  发布在  Java
关注(0)|答案(2)|浏览(490)

我正在尝试为我一直维护的项目设计一个高分菜单,我希望能够使用.txt或.csv文件来存储和检索此项目的所有高分,以显示在我用scenebuilder构建的高分菜单上,但我在这样做时遇到了麻烦。到目前为止,我已经尝试使用传统的方法读取文件,但我似乎无法将其输入到scenebuilder的文本区域。感谢您的帮助。谢谢。
高分.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>

高分.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);
    }
}
pgvzfuti

pgvzfuti1#

你得把它放在箱子里 list ,列表是您的项目源,并且 ObservableList 因此列表中的更改将触发视图中的更改,绑定在: table.setItems(list); ```
ObservableList 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 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);
}
l0oc07j2

l0oc07j22#

您可以使用streamapi加载文件并创建 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));

相关问题