Search code examples
javaswingjtableabstracttablemodel

Use DefaultTableModel and AbstractTableModel together


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


Solution

  • 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