我正在尝试在JTable中添加行

时间:2013-02-28 04:58:28

标签: sql jtable mouseevent variable-assignment

我正在尝试在JTable中添加行,它在我身上运行正常,问题是当我尝试单击JTable中的单元格时,出现错误ArrayIndexOutOfBoundsException: -1。有人可以帮帮我吗?
          PreparedStatement pr = conn.prepareStatement("SELECT * FROM tblInfo");
        ResultSet rs = pr.executeQuery();

        ResultSetMetaData metadata = rs.getMetaData();

        Vector<String> col = new Vector<String>();
        int columnCount = metadata.getColumnCount();

        for(int i=1; i<=columnCount; i++)
        {
        col.add(metadata.getColumnName(i));
        } 

        Vector<Vector<Object>> data = new Vector<Vector<Object>>();
        while(rs.next())
        {
            Vector<Object> vector = new Vector<Object>();
            for(int i=1; i<=columnCount; i++)
            {
                vector.add(rs.getObject(i));
            }
            data.add(vector);
        }


        table = new JTable(data, col);
        table.setBackground(Color.LIGHT_GRAY);
        table.setForeground(Color.white);
        table.setFont(new Font("Ubuntu Light", Font.PLAIN, 16));
        table.setAutoResizeMode(table.AUTO_RESIZE_ALL_COLUMNS);
        table.setRowHeight(22);
        table.setCellSelectionEnabled(false);


        table.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent m){

                Object one, two, three, four;
                int i = table.getSelectedRow();

                int col = table.getSelectedColumn();
                int row = table.getSelectedRow();
                one = GetData(table,i, 0);
                two = GetData(table,i, 1);

                txtDoctorID.setText(one.toString());
                txtDoctorName.setText(two.toString());   

            //  txtDoctorID.setText(table.getModel().getValueAt(row,col).toString());   



            }
        }); 


        scroll = new JScrollPane(this.table);
        scroll.setSize(680,200);
        scroll.setLocation(265,65);
        scroll.setBackground(Color.LIGHT_GRAY);
        scroll.setAutoscrolls(true);
        scroll.setWheelScrollingEnabled(true);
        getContentPane().add(scroll); 

        st.close();
        rs.close();

//我刚删除了try和catch

这是错误提示

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.Vector.elementData(Vector.java:730)
at java.util.Vector.elementAt(Vector.java:473)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:649)
at Record.GetData(Record.java:401)
at Record$1.mouseClicked(Record.java:290)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
at java.awt.Component.processMouseEvent(Component.java:6508)
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:4501)
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:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
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:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
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)

0 个答案:

没有答案