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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:31:26+00:00 2026-06-18T01:31:26+00:00

I have a table that read records from file and display them and have

  • 0

I have a table that read records from file and display them
and have a delete button that when user select a row and clicked, that line delete from table and text file too.

(Updated)

public class Readuser_A extends AbstractTableModel {

String[] columns = { "Fname", "Lname", "Gender", "Date", "ID" };
ArrayList<String> Listdata = new ArrayList<String>();
String[][] Arraydata;

public Readuser_A() {
    try {
        FileReader fr = new FileReader("AllUserRecords.txt");
        BufferedReader br = new BufferedReader(fr);
        String line;
        while ((line = br.readLine()) != null) {
            Listdata.add(line);
        }
        br.close();
        Arraydata = new String[Listdata.size()][];
        for (int i = 0; i < Listdata.size(); i++) {
            Arraydata[i] = Listdata.get(i).split("     ");
        }
    } catch (IOException e) {
    }
}

 public void RemoveMyRow(int row){
  Listdata.RemoveElement(row);
   }

@Override
public String getColumnName(int colu) {
    return columns[colu];

}

public int getRowCount() {
    if (null != Arraydata) {
        return Arraydata.length;
    } else {
        return 0;
    }
}

public int getColumnCount() {
    return columns.length;
}

public Object getValueAt(int rowIndex, int columnIndex) {
    return Arraydata[rowIndex][columnIndex];
}
}

My second Class:

public class ReaduserM_A {
final JLabel myLable = new JLabel();

public ReaduserM_A() {

    final Readuser_A RU = new Readuser_A();
    final JTable mytable = new JTable(RU);
    final JFrame Uframe = new JFrame("All Users");
    JButton DellButton = new JButton("Delete User");

    DellButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (mytable.getSelectedRow() != -1) {
                removeRow(mytable.getSelectedRow());
                RU.fireTableRowsDeleted(mytable.getSelectedRow(),
                        mytable.getSelectedRow());
            } else {
                JOptionPane.showMessageDialog(null, "No Row Selected");
                return;
            }

            //Now, Delete from text file too
            deleteFromFile();
        }

    });

    JPanel panel = new JPanel();
    JScrollPane sp = new JScrollPane(mytable);
    panel.add(sp);
    panel.add(DellButton);
    panel.add(myLable);
    Uframe.add(panel);
    Uframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Uframe.setSize(570, 500);
    Uframe.setLocation(300, 60);
    Uframe.setVisible(true);
}

public void deleteFromFile() {
    File Mf = new File("AllUserRecords.txt");
    File Tf = new File("Uoutput.txt");
    try {
        FileReader Ufr = new FileReader(Mf);
        BufferedReader Ubr = new BufferedReader(Ufr);
        PrintWriter Upw = new PrintWriter(new FileWriter(Tf));
        String Us;
        while ((Us = Ubr.readLine()) != null) {
            String[] Ust = Us.split("     ");
            String Unumber = Ust[4];

            //How find the selected row line by it's ID and delete that row?
        }
        Upw.close();
        Ubr.close();
        Mf.delete();
        Tf.renameTo(Mf);

    } catch (FileNotFoundException e1) {
        myLable.setText("File Not Found");
    } catch (IOException ioe) {
        myLable.setText("IO Error");
        ioe.printStackTrace();
    }
}

public static void main(String[] args) {
    new ReaduserM_A();
}
}

Thank you

  • 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-18T01:31:28+00:00Added an answer on June 18, 2026 at 1:31 am
    removeRow(mytable.getSelectedRow());
    

    Above statement may be the problem. Because if there is no selected row then getSelectedRow returns -1.

    Check whether the row exists or not. Then delete if it exists.

    if(mytable.getSelectedRow() != -1) {
      removeRow(mytable.getSelectedRow());
    }
    

    UPDATE:

    I ran your code I got NullPointerException in getRowCount method of your TableModel class.

    public int getRowCount() {
       return Arraydata.length;
    }
    

    So do a null check before you get the count.

    public int getRowCount() {
    if(null != Arraydata) {
        return Arraydata.length;
    } else {
        return 0;
    }
    }
    

    Now if you run you will get the ArrayOutOfBoundException with index -1. This is because of the delete action. As I stated earlier, check whether row exists or not then do the respective action. The following code does that.

    public void actionPerformed(ActionEvent e) {
        if(mytable.getSelectedRow() != -1) { 
          removeRow(mytable.getSelectedRow());
          rftl2.fireTableRowsDeleted(mytable.getSelectedRow(), mytable.getSelectedRow());
        } else {
          JOptionPane.showMessageDialog(null, "No Row Selected");
           return;
        }
    
        //Now, Delete from text file too
        deleteFromFile();
     }
    

    Finally you get the output (if there is no selected row like this.)

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the database, I have a definition table that is read from the application
I have a database table with a field that I need to read from
I have a function that updates a MySQL table from a CSV file. The
I am New in java, I have a JTable that can read records from
I have table that I insert data with following query (from c# code): INSERT
I have a table that has account numbers in (account_num) and user profiles (profile_id).
I am trying to read 738627 records from a flat file into MySQl. The
I have a single table with lots of records (> 100k) that I need
I have a VB web application that reads from a CSV file which contains
I have a table that contains information pointing to files stored on a file

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.