JTable - 删除行问题

时间:2013-01-29 13:21:59

标签: java swing file-io jtable abstracttablemodel

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

但不行! 我的代码:

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&gt; = 0 表示我选择的表行(我选择了第六行并单击了删除按钮) 你能帮助我更多吗?

谢谢!

1 个答案:

答案 0 :(得分:6)

  1. 学习Java命名约定并坚持使用
  2. 永远不要忽视例外
  3. 您的代码没有意义:您要从课程ReaduserM_A中删除元素,但您的JTable使用另一个Readuser_A的表格模型。你混淆了两种不同的模型。我认为您实际上不应在DefaultTableModel
  4. 中延伸ReaduserM_A
  5. DefaultTableModel中,您无需手动触发事件。它会自动完成所有默认的API方法。