Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9265499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:04:56+00:00 2026-06-18T14:04:56+00:00

I use this method to add a new row to my jtable and file

  • 0

I use this method to add a new row to my jtable and file too.

But when i clicked to add button, That new record added to jtable, but when i see the text file, I found something like this:

myproject.Library.BookInformation@9899472

where is my mistake?

My code:

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();
   }
}

second class for add new record:

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;
}
}

My table model Class:

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;

}
}

My BookInformation Class:

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;
}
}

Thanks for help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:04:57+00:00Added an answer on June 18, 2026 at 2:04 pm

    Looks like what you get in the file is a result of method toString() invocation on an object of this class: myproject.Library.BookInformation.

    So the quickest fix in your case would be to override toString() method for BookInformation to return what you need.

    public String toString() {
        return getBookName(); // Or whatever you see fit.
    }
    

    Even if later you’ll change your code not to rely on toString() a meaningful implementation is not going to hurt.

    Unlike in another answer you do NOT have to change code for AddToFile if you override toString(). However, if you don’t modify code for BookingInformation, then you would have to craft string value when you call AddToFile similar to what was suggested:

    AddToFile(bookinfo.getBookName()) // Or whatever you see fit.
    

    Another even better way would be to modify AddToFile method to accept BookingInformation as a parameter.

    public boolean AddToFile(BookingInformation bookinfos){
        try{       
        PrintWriter Bpw=new PrintWriter(new FileOutputStream("AllBookRecords.txt",true));
        Bpw.println(bookinfos.getBookName()); // Or whatever you see fit.
        return true;
        }
        catch(IOException ioe){
        return false;
        } 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Many people use this method to add animation on switching views. When I try
i use this method before 1 time. but Now i can not get video
We use this small utility method. But we don't like it. Since it's not
I use this method to delete a row in my sqlite db: db.execSQL(delete from
I use this method for changing my table cell value, it change on jtable
I need use this method with three distinct classes: Orders, Customers, Suppliers public void
Trying to use this method (gist of which is use self.method_name in the FunnyHelper
actually i use this method to show similar words for a search request.. $query
I'd like to use this method to create user-friendly URL. Because my site is
When i use this method, its throws illegal state expression on blackberry simulator. protected

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.