-1

I have this persistent error that seems impossible to fix. Basically, the "Remove" jButton removes a Row that is elected in my jTable. Not only that, this jTable is connected to my database. So the function of my jButton should remove the Data from my Data Table and then repaint the Table. But everytime i clicked the button this error shows up. What is wrong w/ my code?

public class:

public class Main extends javax.swing.JFrame {

String a;
String Price;
String Stock;
    public Main() {
        initComponents();
        jTable_products.getSelectionModel().addListSelectionListener(new ListSelectionListener()
        {
            @Override
            public void valueChanged(ListSelectionEvent e)
            {
                int selectedRow = jTable_products.getSelectedRow();
                a = (String) jTable_products.getValueAt(selectedRow, 0);
                 Price=String.valueOf(jTable_products.getValueAt(selectedRow, 1));
                  Stock=String.valueOf(jTable_products.getValueAt(selectedRow, 2));


            }
        }
        );

}  

My "Remove" jButton:

 DefaultTableModel model = (DefaultTableModel) jTable_products.getModel();
            try{

            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/projectephone","root","");
            String sql= "delete from mycart(Model,Price,Stock) values(?,?,?)";
            PreparedStatement pst = con.prepareStatement(sql); 
            pst.setString(1, a);
            pst.setString(2,Price);
            pst.setString(3, Stock);
            model.removeRow(jTable_products.getSelectedRow());
            a="";
            Price="";
            Stock="";

            pst.executeUpdate();
            }
            catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
             repaint();
        try
        {
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/projectephone","root","");
            String sql = "SELECT * from mycart";
            PreparedStatement pst = con.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();
            jTable_products.setModel(DbUtils.resultSetToTableModel(rs));

        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, "Fails to display"+e);
        }
    }                                        

"Clear" jButton:

DefaultTableModel model = (DefaultTableModel)jTable_products.getModel();

    while(model.getRowCount() > 0){
        for(int i = 0; i < model.getRowCount();i++){
            model.removeRow(i);
        }
    }
}

Stack Trace:

xception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.Vector.elementData(Vector.java:734)
    at java.util.Vector.elementAt(Vector.java:477)
    at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:648)
    at javax.swing.JTable.getValueAt(JTable.java:2717)
    at Genos.Main$1.valueChanged(Main.java:32)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:184)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:164)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:211)
    at javax.swing.DefaultListSelectionModel.removeIndexInterval(DefaultListSelectionModel.java:677)
    at javax.swing.JTable.tableRowsDeleted(JTable.java:4509)
    at javax.swing.JTable.tableChanged(JTable.java:4412)
    at javax.swing.table.AbstractTableModel.fireTableChanged(AbstractTableModel.java:296)
    at javax.swing.table.AbstractTableModel.fireTableRowsDeleted(AbstractTableModel.java:261)
    at javax.swing.table.DefaultTableModel.removeRow(DefaultTableModel.java:463)
    at Genos.Main.jButton7ActionPerformed(Main.java:377)
    at Genos.Main.access$700(Main.java:19)
    at Genos.Main$8.actionPerformed(Main.java:155)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    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:6525)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6290)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)  

1 Answers1

1

But when i have click the "Clear" button in my jFrame it shows the Stack Trace

The code works fine for me although the code is very confusing and way more complex than it needs to be.

The DefaultTableModel provides a method that allows you do delete all the rows:

model.setRowCount(0);

No need for the while loop of if loop.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • That is really helpful. Thanks. But what i need from the "Clear" and the "Remove" button is to edit the Data Table itself, not the jTable. So the purpose of "Clear" is to clear out the list stored in my Data Table. – Chester Vacalares Mar 18 '16 at 15:37
  • @ChesterVacalares, I have no idea what the "Data Table" is?. When using Swing your should only ever make changes the to the TableModel. The TableModel will then notify the table to repaint itself. That is why the removeRow() and setRowCount() methods works on the DefaultTableModel. So the solution I suggest is working on the data, not the table directly. – camickr Mar 18 '16 at 15:45