tags:

views:

111

answers:

3

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)
+2  A: 

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.

Brian Agnew
A: 

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?

duffymo
A: 
sateesh