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

  • Home
  • SEARCH
  • 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 9244369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:00:45+00:00 2026-06-18T09:00:45+00:00

My jtable should read a text file and show them. It reads all data

  • 0

My jtable should read a text file and show them.

It reads all data correctly, But just show last line record in file, in its all rows repetitive.

Where is my mistake?

My text file:

uiui     898     666999
vvvv     6666     7777
hfsn     5356     56
ds     232     2212
bbnn     2013     211

My AllBooks Class:

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;

}

}

My AllBooksM Class:

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

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!

  • 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-18T09:00:46+00:00Added an answer on June 18, 2026 at 9:00 am

    You’re using the same BookInformation object with each iteration of the while loop and instead need to create a new one with each iteration. Else that same object will be held by all rows of the table model causing the same information will be displayed on every row.

    For instance you can solve it by doing something like this.

    public BookInformation initializeUserInfos(String str){
        System.out.println(str);
        String[] bookCellArray=str.split("     ");
    
        // create and use a local BookInformation variable and object:
        BookInformation bInfos = new BookInformation(); // *****
    
        bInfos.setBookName(bookCellArray[0]);
        bInfos.setBookDate(bookCellArray[1]);
        bInfos.setBookID(bookCellArray[2]);
        return bInfos;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to read data from a .txt file and send them to my
I have a ScrollPane and JTable that should only have one row of data.
How should I disable just a single column from dragging in JTable? I want
I have a JTable that get's populated with data from a MySQL Table, but
Is there a way to tell a JTable's row filter that it should update
I have a JTable inside a JScrollPane. After calling my function which should populate
Is it possible to add a DateTimePicker to a JTable Cell.A particular column should
I have created one jTable. I want to display the data into table from
I want to create the JTable run time . and it should bind the
I want to display a text above a JTable cell when someone is editing

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.