1

我有一个应该从文件中读取并显示的表,并且可以从表和文件中删除行。

但不行!我的代码:

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];
    }
}

我的第二课:

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

发生此异常:

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)

我认为在第一行,ArrayIndexOutOfBoundsException: 6 >= 0 意味着我选择的表格行(我选择了第六行并单击了删除按钮)你能帮我更多吗?

谢谢!

4

1 回答 1

6
  1. 学习 Java 命名约定并遵守它们
  2. 永远不要默默地解雇异常
  3. 您的代码没有意义:您正在从类中删除一个元素,ReaduserM_A但您JTable使用另一个表模型,即Readuser_A. 您混淆了两种不同的模型。我相信你实际上不应该DefaultTableModel扩展ReaduserM_A
  4. DefaultTableModel您不需要手动触发事件。对于所有默认 API 方法,它会自动完成。
于 2013-01-29T13:37:46.990 回答