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 9051353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:53:07+00:00 2026-06-16T12:53:07+00:00

I am New in java, I have a JTable that can read records from

  • 0

I am New in java, I have a JTable that can read records from a txt file and show they perfectly.
I want to add a new book to my JFrame that when user select a row on table and clicked the “delete” button, that row should delete and that deleted row records must delete from txt file,too.
my code is this, but it has errors and not seen JTable! :

public class CopyOfAllUserTable extends AbstractTableModel {
Vector data;
Vector column;
public static void main(String[] args){
    new CopyOfAllUserTable();
}
public CopyOfAllUserTable() {

        String line;
        data = new Vector();
        column = new Vector();
        try {
        FileInputStream fis = new FileInputStream("D:\\AllUserRecords.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(fis));
      StringTokenizer st1 = new StringTokenizer(br.readLine(), " ");
              while (st1.hasMoreTokens())
               column.addElement(st1.nextToken());
                while ((line = br.readLine()) != null) {
                       StringTokenizer st2 = new StringTokenizer(line, " ");
                        while (st2.hasMoreTokens())
                                data.addElement(st2.nextToken());
                }
                br.close();
        } catch (Exception e) {
                e.printStackTrace();
        }

    final JFrame frame1=new JFrame();
     JTable table=new JTable(data,column);
    JButton button1=new JButton("Delete");


    button1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
           DefaultTableModel model=new DefaultTableModel(data, column);
           JTable table=new JTable(model);

        }
    });


    JPanel panel=new JPanel();
    panel.add(table);
    panel.add(button1);
    frame1.add(panel);
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.setBounds(200, 80, 600, 500);
    frame1.setVisible(true);
    frame1.setEnabled(true);


}

public int getRowCount() {
        return data.size() / getColumnCount();
}

public int getColumnCount() {
        return column.size();
}

public Object getValueAt(int rowIndex, int columnIndex) {
        return (String) data.elementAt((rowIndex * getColumnCount())
                        + columnIndex);
}

}

My problem is in delete row, and read records from file to jtable are perfectly successful.

  • 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-16T12:53:08+00:00Added an answer on June 16, 2026 at 12:53 pm

    Firstly you’re not adding your JTable to the content of the frame.

    For containers like: frame.getContentPane() and JPanel you should add the child components by using their #add(...) method.

    For example:

     final JPanel panel=new JPanel(new BorderLayout());
     button1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
           DefaultTableModel model=new DefaultTableModel(data, column);
           JTable table=new JTable(model);
           panel.add(new JScrollPane(table));
           panel.revalidate();
        }
    });
    

    Note that JPanel default layout is FlowLayout. Second thing – if you want to have headers and scrolling in your JTable you need to wrap it with JScrollPane.
    Next – you should revalidate the panel after adding/removing/etc.

    The second issue is removing rows from JTable. I usually write a method to handle it:

    protected void removeRows(final int[] rows) {
        int modelRows[] = new int[rows.length];
        for(int i = 0; i < rows.length; ++i) {
            modelRows[i] = table.convertRowIndexToModel(rows[i]);
        }
        Arrays.sort(modelRows);
        for(int i = modelRows.length - 1; i >= 0; --i) {
            int row = modelRows[i];
            model.removeRow(row);
        }
        model.fireTableDataChanged();
    }
    

    The convertRowIndexToModel method converts index returned by JTable#getSelectedRows() or JTable#getSelectedRow() (which are the visible indices) to the model indices. If you set RowSorter for your JTable or you leave it to standard implementation:

    table.setAutoCreateRowSorter(true);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a a table that show all book list from a txt file
I am new to Java. I just read that class variables in Java have
I have a simple Java program that reads in a text file, splits it
i have jtable filed with data. i want to create java code for Action
I have written a code that can save data into xml. NOW I want
I have a GUI in java swing with a JTable and JButtons that implements
i have one jtable with listselectionlistener i can dynamically added the new rows in
I have a standalone Java application that gets data from a database and displays
I'm still fairly new to Java and have a question in regards to JTable
I have a code that read file and store in arraylist and then convert

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.