我也使用此方法向我的 jtable 和文件添加新行。
但是当我单击添加按钮时,该新记录已添加到 jtable 中,但是当我看到文本文件时,我发现如下内容:
myproject.Library.BookInformation@9899472
我的错误在哪里?
我的代码:
public class MainS extends JFrame{
final AllBooks allBooks=new AllBooks();
final JTable Btable=new JTable(allBooks);
public MainS(){
JButton AddBookButton=new JButton("Add New Book");
AddBookButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
AddDialogS adddialog=new AddDialogS(MainS.this);
adddialog.setVisible(true);
BookInformation B_info=adddialog.getBookInfos();
if(B_info != null){
allBooks.AddRow(B_info);
}
}
});
JPanel Bpanel=new JPanel();
Bpanel.setLayout(new FlowLayout());
JScrollPane sp=new JScrollPane(Btable);
Bpanel.add(sp);
Bpanel.add(AddBookButton);
this.add(Bpanel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(300, 60, 550, 550);
this.setVisible(true);
}
public static void main(String[] args){
new MainS();
}
}
添加新记录的第二类:
public class AddDialogS extends JDialog{
BookInformation bookinform=new BookInformation();
public AddDialogS(JFrame owner){
super(owner,"Add New Book!", true);
JButton OkButton=new JButton("Ok");
final JTextField nameTF=new JTextField(10);
JLabel namelbl=new JLabel("bookname");
final JTextField dateTF=new JTextField(10);
JLabel datelbl=new JLabel("bookDate");
final JTextField idTF=new JTextField(10);
JLabel IDlbl=new JLabel("bookId");
OkButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
bookinform.setBookName(nameTF.getText().trim());
bookinform.setBookDate(String.valueOf(dateTF.getText().trim()));
bookinform.setBookID(String.valueOf(idTF.getText().trim()));
AddDialogS.this.dispose();
}
});
JPanel panel=new JPanel(new FlowLayout());
panel.add(OkButton);
panel.add(nameTF);
panel.add(dateTF);
panel.add(idTF);
panel.add(namelbl);
panel.add(datelbl);
panel.add(IDlbl);
this.add(panel);
this.setBounds(10, 30, 400, 500);
}
public BookInformation getBookInfos(){
return bookinform;
}
}
我的表模型类:
public class AllBooks extends AbstractTableModel{
BookInformation Binfos1=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(initializeBookInfos(line));
}
br.close();
}
catch(IOException ioe){
}
}
public static BookInformation initializeBookInfos(String str){
BookInformation Binit=new BookInformation();
String[] bookCellArray=str.split(" ");
Binit.setBookName(bookCellArray[0]);
Binit.setBookDate(bookCellArray[1]);
Binit.setBookID(bookCellArray[2]);
return Binit;
}
public void AddRow(BookInformation bookinfo){
if(AddToFile(bookinfo)){
Bdata.add(bookinfo);
fireTableRowsInserted(Bdata.size()-1, Bdata.size()-1);
}
else{
JOptionPane.showMessageDialog(null, "Unable Add To File"+bookinfo.getBookName());
}
}
public boolean AddToFile(String bookinfos){
try{
PrintWriter Bpw=new PrintWriter(new FileOutputStream("AllBookRecords.txt",true));
Bpw.println(bookinfos);
return true;
}
catch(IOException ioe){
return false;
}
}
@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;
}
}
我的 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;
}
}
感谢帮助。