如何从GUI表中删除数据?

时间:2009-12-13 12:47:37

标签: java user-interface

我有这个框架,我想删除它的表数据,然后输入一些数据, 但我有这个例外的原因吗?请帮帮我。

我的出生清单框架:

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;
}}

堆栈跟踪:

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)

3 个答案:

答案 0 :(得分:2)

我会从基础TableModel中删除数据并调用fireTableDataChanged()方法。有关表模型如何工作的教程,请参阅here

请注意,存在其他方法用于警告行已删除或故事结构已更改,并且(根据您的具体情况)这些可能更适合使用。

答案 1 :(得分:0)

堆栈跟踪告诉您需要知道的一切:

  

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

您传入的是jtable1集合中不存在的索引。使用调试器进行快速浏览应该可以解决问题。

jtable1在哪里声明?我错过了吗?

答案 2 :(得分:0)

我发现你的代码中还有一个差异(虽然与你得到的异常无关)。

你有类似的东西:


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

但是你的类没有扩展Swing Framework中提供的任何TableModel类(AbstractTableModel或DefaultTableModel)来创建你自己的TableModel,所以这个方法似乎没有任何用途。此外,上述方法(在Swing类中)的默认实现是始终返回true 而且你的代码也是这样做的。所以可能不需要上面的方法。