java - 如何将数据添加到 JavaFX 表行/单元格

标签 java javafx javafx-8

我有一个 JavaFX 表,我想向每一行添加一些数据。数据来自文本文件。 这是应该将每个数据 block 输入到特定单元格的代码 文件中的每一行都采用以下格式:

firstName;LastName;SchoolName;email;year;active?;amountOwed;

基本上每一行都是表中的一行。

try{
        ArrayList<String> allLinesOfTheFile= new ArrayList<String>();
        String path = "G:\\";
        String line;
        File masterFile = new File(path,"masterfile.txt");
        BufferedReader readMasterFile = new BufferedReader(new FileReader(masterFile));

        while ((line = readMasterFile.readLine()) != null) {

            for (String data: line.split(";")){

                //this splits the data into individual pieces, each piece will take up one cell in the table

            }

        }


    }

Here is the tableview

最佳答案

您必须创建一个模型类来保存这些值。这是一个您可以使用的小示例

模型类:

public class Model {

    private String col1;
    private String col2;

    public Model(String[] st){
        this.col1 = st[0];
        this.col2 = st[1];
    }
    public String getCol1() {
        return col1;
    }

    public void setCol1(String col1) {
        this.col1 = col1;
    }

    public String getCol2() {
        return col2;
    }

    public void setCol2(String col2) {
        this.col2 = col2;
    }

}

您的 Controller 类

  ObservableList<Model> lst = FXCollections.observableArrayList();
    String path = "G:\\";
    String line;
    File masterFile = new File(path,"masterfile.txt");
    BufferedReader readMasterFile = new BufferedReader(new FileReader(masterFile));

    while ((line = readMasterFile.readLine()) != null) {

          String[] data =line.split(";");
          Model md= new Model(data);
          lst.add(md);
   }
   table.setItems(lst);

关于java - 如何将数据添加到 JavaFX 表行/单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33952485/

相关文章:

java - GAE JDO - 建模这些关系的正确方法是什么?

java - 避免Java因证书错误而出现安全警告

java - android :weight in JavaFX的对应

javafx - 在对话框中的按钮下添加文本

java - 无法在 JavaFX 组合框中转换对象

java - 使用 ObservableMap 填充并同步 ComboBox?

Javafx文本区域滚动 Pane 边框颜色问题

Java:在嵌套列表中匹配列表

java - 将相机图像保存到 SD 卡,检索并上传到 Firestore 存储

java - 为什么我不需要导入 android "R"类?