【发布时间】:2016-09-20 11:58:33
【问题描述】:
我需要一些关于 JTable 的帮助。我正在尝试从“Contact.txt”文件中读取数据并使用该数据填充我的表。我可以通过添加到 Object[][] 2D 数组来正确地从文件中获取数据没有任何问题。当我尝试将此对象数组添加到表中时不会发生任何事情。 对不起我的英语不好。
Contacts.txt 文件包括“姓名”、“姓氏”、“电话号码”、“电子邮件”等。
这个类读取文本并将其添加到 Object :
public class ReadFromText {
public boolean ReadTable(Object [][] data) {
boolean status = false;
File file = new File("/Users/MacbookPro/Documents/Contacts.txt");
BufferedReader bf = null;
FileReader fileReader = null;
try {
fileReader = new FileReader(file);
bf = new BufferedReader(fileReader);
String textLine = null;
String [] text = null;
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < 4; j++) {
while ((textLine = bf.readLine()) != null) {
text = textLine.split(" ");
data[i][j] = text[j];
status = true;
System.out.println(data[i][j]);
}
}
}
bf.close();
} catch (IOException e) {
System.err.println(e.getMessage());
}
return status;
}
}
这部分代码来自main Class:
Object [][]datas = new Object[10][4];
ReadFromText r = new ReadFromText(); //new object from ReadData class
if(r.ReadTable(datas)== true){
System.out.println("OK");//just for to be sure
}else{
System.out.println("NO");
}
model = new DefaultTableModel(datas, columNames);
table = new JTable(model);
table.setFont(new Font("Monospaced", Font.PLAIN, 13));
table.setBackground(new Color(245, 245, 245));
table.setRowHeight(25);
table.setMinimumSize(new Dimension(60, 20));
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
从here返回“OK”检查它
但是 JTable 是空的!!! : 看this picture
我希望任何人都可以帮助我。感谢所有人
【问题讨论】:
-
打印出数据会得到什么?
-
@c0der 感谢重播,但我试过了。你可以从第二张图片中看到结果
标签: java arrays swing file-io jtable