1

I have a table that should read from file and display and can delete row from table and file.

But not work! My code:

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) {
        }
    }

    @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 extends DefaultTableModel {
    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();
    }
}

This Exception occur :

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 6 >= 0
    at java.util.Vector.removeElementAt(Vector.java:554)
    at javax.swing.table.DefaultTableModel.removeRow(DefaultTableModel.java:463)
    at Array.ReaduserM_A$1.actionPerformed(ReaduserM_A.java:33)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:696)
    at java.awt.EventQueue$4.run(EventQueue.java:694)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

I think that In First Line, ArrayIndexOutOfBoundsException: 6 >= 0 mean that my table row that selected(I selected sixth row and clicked delete button) Can you help me more?

Thanks!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user1945649
  • 35
  • 1
  • 7

1 Answers1

6
  1. Learn Java naming conventions and stick to them
  2. Never dismiss silently Exceptions
  3. Your code does not make sense: you are removing an element from the class ReaduserM_A but your JTable use another table model which is Readuser_A. You are confusing two different models. I believe that you should actually not extend DefaultTableModel in ReaduserM_A
  4. In DefaultTableModel you don't need to fire manually events. It is done automatically for all the default API methods.
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • +1 - His core problem is that he is extending DefaultTableModel for his main class. So when he calls `removeRow` - everything blows up. – Perception Jan 29 '13 at 13:44
  • I need to your help so much – user1945649 Jan 29 '13 at 13:48
  • @user1945649 remove `extends DefaultTableModel` from your second class, implement a method `removeRow` in `Readuser_A` (don't forget to fire the appropriate table model event provided by `AbstractTableModel`) and invoke that new method from your second class. It will be probably easier to have your table model data based on List>` rather than `String[][]` – Guillaume Polet Jan 29 '13 at 13:52
  • I try your solution and tell result. – user1945649 Jan 29 '13 at 13:53