我有一个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
}
}
}
答案 0 :(得分:0)
您必须创建一个模型类来保存值。这是一个可以使用的小例子
模特课程:
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);