簡體   English   中英

如何將數據添加到JavaFX Table行/單元格

[英]How to add data to JavaFX Table rows/cells

我有一個JavaFX表,我想向每行添加一些數據。 數據來自文本文件。 這是應該將每個數據段輸入到特定單元格中的代碼,文件中的每一行都采用以下格式:

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

            }

        }


    }

這是表格視圖

您必須創建一個模型類來保存值。 這是一個可以使用的小例子

型號類別:

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;
    }

}

您的控制器類

  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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM