简体   繁体   English

如何从GUI表中删除数据?

[英]How can I delete data from my GUI table?

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)

I would remove the data from the underlying TableModel and invoke the fireTableDataChanged() method. 我将从底层TableModel中删除数据,然后调用fireTableDataChanged()方法。 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. 请注意,还存在其他方法来警告行已被删除或故事结构已更改,并且(取决于您的情况)这些方法可能更适合使用。

The stack trace tells you everything you need to know: 堆栈跟踪告诉您需要了解的所有信息:

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

You're passing in an index that doesn't exist in your jtable1 collection. 您正在传递jtable1集合中不存在的索引。 A quick spin through with a debugger should sort you right out. 使用调试器快速浏览应该可以使您分清。

Where is jtable1 declared? jtable1在哪里声明? Did I miss it? 我想念吗?

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. 但是您的类不会扩展Swing框架(AbstractTableModel或DefaultTableModel)中提供的任何TableModel类来创建自己的TableModel,因此该方法似乎没有任何作用。 Also the default implementation of the above method (in the Swing classes) is to always return true 同样,上述方法的默认实现(在Swing类中)总是返回true
and your code is also just doing the same. 而且您的代码也正在这样做。 So probably the above method isn't needed. 因此,可能不需要上述方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM