我有一个 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
}
}
}
最佳答案
您必须创建一个模型类来保存这些值。这是一个您可以使用的小示例
模型类:
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/