koobin
koobin

Reputation: 5

From database into JTable

My code show data from database into table correctly But when i want to delete a row, Row is delete from database and table model,

But a "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1 exception occur:

public class Model1 extends DefaultTableModel {

public Model1(Vector<Vector<String>> data, Vector<String> column) {
    super(data, column);
}

public void removeRow(int rowToModel, Object rowId) {
    JDBC1 jdbc1 = new JDBC1();

    try {
        Connection con;
        PreparedStatement ps = null;
        con = DriverManager.getConnection(...);

        ps = con.prepareStatement("delete from table where id=?");
        ps.setObject(1, rowId);

        if (ps.executeUpdate() == 1) {
            jdbc1.getColumns().remove(rowToModel);
            fireTableRowsDeleted(rowToModel, rowToModel);
        }

    } catch (SQLException sqle) {
        sqle.printStackTrace();
    }
}
}

My JDBC Class:

public class JDBC1 {

private Vector<String> columns = new Vector<String>();
Vector<Vector<String>> data = new Vector<Vector<String>>();

Connection con;
Statement statement;
ResultSet result;
String query = "Select * from table";

public Vector<String> getColumn() throws SQLException {

    con = DriverManager.getConnection(...);
    statement = con.createStatement();
    result = statement.executeQuery(query);

    int c = result.getMetaData().getColumnCount();
    for (int i = 1; i <= c; i++) {
        getColumns().add(result.getMetaData().getColumnName(i));
    }
    return getColumns();
}

public Vector<Vector<String>> getData() throws SQLException{
    con = DriverManager.getConnection(...);
    statement = con.createStatement();
    result = statement.executeQuery(query);
    int c = result.getMetaData().getColumnCount();

    while (result.next()) {
        Vector<String> newRow = new Vector<String>(c);

        for (int i = 1; i <= c; i++) {
            newRow.add(result.getString(i));
        }
        data.add(newRow);
    }
    return data;

}

public Vector<String> getColumns() {
    return columns;
}

public void setColumns(Vector<String> columns) {
    this.columns = columns;
}
}

GUI Class:

public class GUI1 extends JFrame implements ActionListener {

JTable table;
Model1 model1;
JDBC1 jdbc1;
JButton dellButton;

public GUI1() {
    try {
        jdbc1 = new JDBC1();
        model1 = new Model1(jdbc1.getData(), jdbc1.getColumn());
        table = new JTable(model1);
    } catch (SQLException sqle) {
        sqle.printStackTrace();
    }
    add(new JScrollPane(table), BorderLayout.CENTER);
    add(dellButton() , BorderLayout.SOUTH);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(600, 550);
    setLocation(250, 85);
    setVisible(true);
}

public static void main(String[] args) {
    new GUI1();
}

public JPanel dellButton(){
    JPanel panel = new JPanel();
    dellButton = new JButton("Delete Row");
    dellButton.addActionListener(this);
    panel.add(dellButton);
    return panel;
}

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == dellButton) {
        dellAction();
    }
}

public void dellAction() {
    if (table.getSelectedRow() > -1) {
        int rowToDelete = table.getSelectedRow();
        int rowToModel = table.convertRowIndexToModel(rowToDelete);
        Object rowId = table.getValueAt(table.getSelectedRow(), 0);
        model1.removeRow(rowToModel, rowId);

    } else JOptionPane.showMessageDialog(null, "Select Row");
}
}

Output:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1
at java.util.Vector.remove(Vector.java:827)
at test.Model1.removeRow(Model1.java:29)
at test.GUI1.dellAction(GUI1.java:76)
at test.GUI1.actionPerformed(GUI1.java:67)
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)

Upvotes: 0

Views: 355

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

Your seem to mixing your column information with your data.

The columns describe, well, the columns, that appear horizontally across the table, where the rows describe the actually data (structured to fit into the columns)...

Instead of...

jdbc1.getColumns().remove(rowToModel);

Try...

removeRow(rowToModel);

This will relieve you of the need to call fireTableRowsDeleted yourself...

Observation

Personally, I would try a limit the amount of information that the dellAction method needs in order to delete a row.

What I mean is, the method already knows the row to be deleted, it should not have to lookup the ID as well. This suggests that the dellAction method knows something about the structure of the view, which may change in the future.

Instead, personally, I would extract the ID from the row data within in the removeRow method itself. This allows the model the opportunity to model the ID or not, based on your needs...

IMHO

Upvotes: 2

Related Questions