我的 jtable 应该读取一个文本文件并显示它们。
它正确读取所有数据,但只显示文件中的最后一行记录,在其所有行中重复。
我的错误在哪里?
我的文本文件:
uiui 898 666999
vvvv 6666 7777
hfsn 5356 56
ds 232 2212
bbnn 2013 211
我的 AllBooks 课程:
public class AllBooks extends AbstractTableModel{
BookInformation Binfos=new BookInformation();
String[] Bcol=new String[]{"Name","Date","Id"};
List<BookInformation> Bdata=new ArrayList<BookInformation>();
public AllBooks(){
try{
FileReader fr=new FileReader("AllBookRecords.txt");
BufferedReader br=new BufferedReader(fr);
String line;
while( (line=br.readLine()) !=null){
Bdata.add(initializeUserInfos(line));
}
br.close();
}
catch(IOException ioe){
}
}
public BookInformation initializeUserInfos(String str){
System.out.println(str);
String[] bookCellArray=str.split(" ");
Binfos.setBookName(bookCellArray[0]);
Binfos.setBookDate(bookCellArray[1]);
Binfos.setBookID(bookCellArray[2]);
return Binfos;
}
@Override
public String getColumnName(int col){
return Bcol[col];
}
@Override
public int getRowCount() {
if(Bdata !=null){
return Bdata.size();
}
else{
return 0;
}
}
@Override
public int getColumnCount() {
return Bcol.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
BookInformation binfo=Bdata.get(rowIndex);
Object value;
switch(columnIndex){
case 0:
value=binfo.getBookName();
break;
case 1:
value=binfo.getBookDate();
break;
case 2:
value=binfo.getBookID();
break;
default :
value="...";
}
return value;
}
}
我的 AllBooksM 课程:
public class AllBooksM {
final AllBooks rbftl=new AllBooks();
final JFrame Bframe=new JFrame("All Book List");
final JTable Btable=new JTable(rbftl);
public AllBooksM(){
JPanel Bpanel=new JPanel();
Bpanel.setLayout(new FlowLayout());
JScrollPane sp=new JScrollPane(Btable);
Bpanel.add(sp);
Bframe.add(Bpanel);
Btable.setAutoCreateRowSorter(true);
Bframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Bframe.setBounds(300, 60, 550, 550);
Bframe.setResizable(false);
Bframe.setVisible(true);
}
public static void main(String[] args){
new AllBooksM();
}
}
我的 BookInformation 类:
public class BookInformation {
private String BookName;
private String BookDate;
private String BookID;
public String getBookName() {
return BookName;
}
public void setBookName(String book_name) {
this.BookName = book_name;
}
public String getBookDate() {
return BookDate;
}
public void setBookDate(String book_date) {
this.BookDate = book_date;
}
public String getBookID() {
return BookID;
}
public void setBookID(String Book_id) {
this.BookID = Book_id;
}
}
谢谢!