我的jtable應讀取文本文件並顯示它們。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;
}
}
謝謝!
請學習Java命名約定並遵守它們。變量以小寫字母開頭!這會讓你的代碼更難以閱讀。如果您使用IDE,則會在您的代碼中看到警告。 –
嗨,變量名應該怎麼樣? – Sajad
他們應該以小寫字母開頭:'bookID'而不是'BookID',例如 –