-1

I have this frame and I want to delete its table data and then enter some data, but I have this exception why? Please help me.

My BirthList frame:

public class BirthList extends javax.swing.JFrame {

private Admin admin;
List<Birth> list;

/** Creates new form BirthList */
public BirthList(Admin admin) {
    initComponents();
    this.admin = admin;
    Manager.admin = admin;

    try {
        Manager.addToBirthListFromMySQL();
    } catch (SQLException ex) {
        Logger.getLogger(BirthList.class.getName()).log(Level.SEVERE, null, ex);
    }
    getDataFromGUITable();

    fillTable();

}

private void getDataFromGUITable() {
    list = new ArrayList<Birth>();
    for (int i = 0; i < jTable1.getRowCount(); i++) {
        deleteAllRows(i);

    }
    admin.setCheckBirth(list);
}

private void deleteAllRows(int row) {
    jTable1.remove(row);

}
 public void fillTable() {

    String[] columNames = {"name", "family", "father's name", "mother's name", "date of birth", "place of birth"};
    List<Birth> birth = admin.getBirthList();
    if (birth.isEmpty()) {
        JOptionPane.showMessageDialog(this, "Death list is empty! at first ,add a person.", "Error", JOptionPane.ERROR_MESSAGE);
    }


    else{

    Object[][] data = new Object[birth.size()][columNames.length];
    for (int i = 0; i < data.length; i++) {
        Birth birth1 = birth.get(i);

        data[i][0] = birth1.getName();
        data[i][1] = birth1.getFamily();
        data[i][2] = birth1.getFatherName();
        data[i][3] = birth1.getMotherName();
        data[i][4] = birth1.getDateOfBirth();
        data[i][5] = birth1.getPlaceOfBirth();


    }
    DefaultTableModel model = new DefaultTableModel(data, columNames);
    jTable1.setModel(model);
}
}
public boolean isCellEditable(int row, int col) {
    return true;
}}

stacktrace:

run:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1
        at java.awt.Container.remove(Container.java:1132)
        at AdminGUI.BirthList.deleteAllRows(BirthList.java:61)
        at AdminGUI.BirthList.getDataFromGUITable(BirthList.java:54)
        at AdminGUI.BirthList.<init>(BirthList.java:45)
        at AdminGUI.BirthFrame.newButton1ActionPerformed(BirthFrame.java:127)
        at AdminGUI.BirthFrame.access$000(BirthFrame.java:21)
        at AdminGUI.BirthFrame$1.actionPerformed(BirthFrame.java:58)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6038)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
        at java.awt.Component.processEvent(Component.java:5803)
        at java.awt.Container.processEvent(Container.java:2058)
        at java.awt.Component.dispatchEventImpl(Component.java:4410)
        at java.awt.Container.dispatchEventImpl(Container.java:2116)
        at java.awt.Component.dispatchEvent(Component.java:4240)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
        at java.awt.Container.dispatchEventImpl(Container.java:2102)
        at java.awt.Window.dispatchEventImpl(Window.java:2429)
        at java.awt.Component.dispatchEvent(Component.java:4240)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
1

3 Answers 3

2

I would remove the data from the underlying TableModel and invoke the fireTableDataChanged() method. See here for a tutorial on how the table model works.

Note that other methods exist for alerting that rows have been deleted or that the tale structure has changed, and (depending on your circumstances) these may be more appropriate to use.

0

The stack trace tells you everything you need to know:

at AdminGUI.BirthList.deleteAllRows(BirthList.java:61)

You're passing in an index that doesn't exist in your jtable1 collection. A quick spin through with a debugger should sort you right out.

Where is jtable1 declared? Did I miss it?

0

I see one more discrepancy in your code (though unrelated to the exception you are getting).

You have something like :


public boolean isCellEditable(int row, int col) { return true; }

But your class doesn't extend any of the TableModel classes provided in the Swing Framework (either of AbstractTableModel,or DefaultTableModel) to create your own TableModel, so the method doesn't seem to serve any purpose. Also the default implementation of the above method (in the Swing classes) is to always return true
and your code is also just doing the same. So probably the above method isn't needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.