我在 Netbeans 上制作了一个小型 UI,并在单独的类中实现了代码(动作侦听器等)。我正在尝试使用存储在 MySql 数据库中的数据刷新 JTable,但是在运行时出现此错误主类,问题不在于 JDBC 连接,它工作正常,但它没有将数据打印到 JTable。
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.Hashtable$Enumerator.next(Hashtable.java:1031)
at com.sun.java.swing.plaf.nimbus.NimbusStyle.compileDefaults(NimbusStyle.java:378)
at com.sun.java.swing.plaf.nimbus.NimbusStyle.validate(NimbusStyle.java:297)
at com.sun.java.swing.plaf.nimbus.NimbusStyle.getValues(NimbusStyle.java:927)
at com.sun.java.swing.plaf.nimbus.NimbusStyle.getInsets(NimbusStyle.java:605)
at javax.swing.plaf.synth.SynthStyle.installDefaults(SynthStyle.java:896)
at javax.swing.plaf.synth.SynthLookAndFeel.updateStyle(SynthLookAndFeel.java:275)
at javax.swing.plaf.synth.SynthLabelUI.updateStyle(SynthLabelUI.java:53)
at javax.swing.plaf.synth.SynthLabelUI.installDefaults(SynthLabelUI.java:48)
at javax.swing.plaf.basic.BasicLabelUI.installUI(BasicLabelUI.java:307)
at javax.swing.JComponent.setUI(JComponent.java:662)
at javax.swing.JLabel.setUI(JLabel.java:242)
at javax.swing.JLabel.updateUI(JLabel.java:256)
at javax.swing.table.DefaultTableCellRenderer.updateUI(DefaultTableCellRenderer.java:144)
at javax.swing.JLabel.<init>(JLabel.java:145)
at javax.swing.JLabel.<init>(JLabel.java:216)
at javax.swing.table.DefaultTableCellRenderer.<init>(DefaultTableCellRenderer.java:94)
at sun.swing.table.DefaultTableCellHeaderRenderer.<init>(DefaultTableCellHeaderRenderer.java:32)
at javax.swing.table.JTableHeader.createDefaultRenderer(JTableHeader.java:610)
at javax.swing.table.JTableHeader.initializeLocalVars(JTableHeader.java:632)
at javax.swing.table.JTableHeader.<init>(JTableHeader.java:138)
at javax.swing.JTable.createDefaultTableHeader(JTable.java:5586)
at javax.swing.JTable.initializeLocalVars(JTable.java:5520)
at javax.swing.JTable.<init>(JTable.java:614)
at javax.swing.JTable.<init>(JTable.java:540)
at AddressBookGui.initComponents(AddressBookGui.java:29)
at AddressBookGui.<init>(AddressBookGui.java:16)
at AddressBookImpl.refreshTable(AddressBookImpl.java:33)
at AddressBookImpl.startGUI(AddressBookImpl.java:26)
at AddressBookImpl.main(AddressBookImpl.java:16)
编辑:是的,对不起,这里是调用它的代码,实际的 GUI 类是从 Netbeans 生成的,我没有触及它,所以除非真的有必要,否则我不会发布那个怪物。
public class AddressBookImpl extends AddressBookGui implements ActionListener {
public static void main(String[] args) {
AddressBookImpl impl = new AddressBookImpl();
impl.startGUI();
impl.listeners();
}
public void startGUI(){
AddressBookGui gui = new AddressBookGui();
gui.main(null);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setResizable(true);
this.setTitle("Address Book");
refreshTable();
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public DefaultTableModel refreshTable() {
AddressBookGui gui = new AddressBookGui();
try{
DatabaseImpl dbi = new DatabaseImpl();
dbi.refreshDatabase();
DefaultTableModel defaultTableModel = new DefaultTableModel();
ResultSetMetaData md = dbi.refreshResult.getMetaData();
int columnCount = md.getColumnCount();
while(dbi.refreshResult.next())
{
Object [] rowData = new Object [columnCount];
for (int i = 0; i < columnCount; i++)
{
rowData[i] = dbi.refreshResult.getObject(i+1);
}
((DefaultTableModel) gui.mainTable.getModel()).insertRow(dbi.refreshResult.getRow() -1, rowData);
}
defaultTableModel.fireTableDataChanged();
return defaultTableModel;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}