因此,当我单击一个按钮(“开放课程”)时,将出现另一个类的JFrame(使用.setVisible(true))。在该JFrame中,有一个JTable(tblOpenLesson)和两个按钮(btnOpenLesson和btnCancel)。
现在我要发生的是,当我单击“打开课程”按钮时,它将执行以下操作:1.更新表以将sqlite数据库(课程)反映到JTable中。2.通过获取文件从表中打开所选文件名称(与数据库中的名称相同),并将其用于FileReader函数。
但是,这给了我错误:
线程“ AWT-EventQueue-0”中的异常java.lang.ArrayIndexOutOfBoundsException:javax.java.util.Vector.elementData(Vector.java:737)处为java.util.Vector.elementAt(Vector.java:480)处为-1 mainWindow.openLesson.access $ 000(openLesson.java:19)上mainWindow.openLesson.btnOpenLessonPopUpActionPerformed(openLesson.java:135)上的.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:648)at mainWindow.openLesson $ 1.actionPerformed( openLesson.java:60),位于javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022),javax.swing.AbstractButton $ Handler.actionPerformed(AbstractButton.java:2348),javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java) :402),位于javax.swing.plaf处的javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)。java.awt.Component.processMouseEvent(Component.java:6539)的basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)在java.awt.Component.processEvent的javax.swing.JComponent.processMouseEvent(JComponent.java:3324) (Component.java:6304)at java.awt.Container.processEvent(Container.java:2239)at java.awt.Component.dispatchEventImpl(Component.java:4889)at java.awt.Container.dispatchEventImpl(Container.java: 2297)在java.awt.Component.dispatchEvent(Component.java:4711)在java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)在java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)在java。位于java.awt.Window的java.awt.Container.dispatchEventImpl(Container.java:2283)的awt.LightweightDispatcher.dispatchEvent(Container.java:4476)。在Java.awt.EventQueue.access $ 500(EventQueue。 java.awt.EventQueue $ 3.run(java:97)(java.awt.EventQueue $ 3.run(EventQueue.java:703)at java.security.AccessController.doPrivileged(Native Method) Java上的.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)在java.awt.EventQueue $ 4.run(ProtectionDomain.java:84)在java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)在Java .awt.EventQueue $ 4.run(EventQueue.java:731)在java.security.AccessController.doPrivileged(本机方法)在java.security。java.awt.EventQueue.dispatchEvent(EventQueue.java:730)处的ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)在java.awt.EventDispatchThread.pumpEventsForFilter处在java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)处(EventDispatchThread.java:116)在java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)在java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)在java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java: 93)在java.awt.EventDispatchThread.run(EventDispatchThread.java:82)205),位于java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116),位于java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101),位于java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)。 awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)205),位于java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116),位于java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101),位于java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)。 awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
我试图寻找解决办法,但在这里停留了3天却无济于事。
这是弹出式JFrame的代码:
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
// </editor-fold>
private void btnCancelOpenLessonActionPerformed(java.awt.event.ActionEvent evt) {
this.setVisible(false);
}
private void btnOpenLessonPopUpActionPerformed(java.awt.event.ActionEvent evt) {
updateDBlessons(); // update list
String[] fileName = null;
int selectedColumn = tblOpenLesson.getSelectedColumn();
int selectedRow = tblOpenLesson.getSelectedRow();
try {
Class.forName("org.sqlite.JDBC");
Connection con = DriverManager.getConnection("jdbc:sqlite:sql_items.sqlite");
Statement stmtGetLesson = con.createStatement();
ResultSet rs = stmtGetLesson.executeQuery("SELECT * FROM lessons WHERE lesson_id = '" + tblOpenLesson.getModel().getValueAt(selectedRow, selectedColumn) + "';");
if (rs.next()) {
fileName[0] = rs.getString("lesson");
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e);
} catch (ClassNotFoundException ex) {
Logger.getLogger(openLesson.class.getName()).log(Level.SEVERE, null, ex);
}
try {
FileReader fr = new FileReader("Lessons\\" + fileName[0] + ".txt");
try (BufferedReader br = new BufferedReader(fr)) {
mainScreen.lessonPane.read(br, null); // lessonPane is from another class
br.close();
}
} catch (IOException e) {
System.out.println(e);
this.setVisible(false);
}
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(() -> {
new openLesson().setVisible(false);
});
}
public void updateDBlessons() {
try {
Class.forName("org.sqlite.JDBC");
Connection con = DriverManager.getConnection("jdbc:sqlite:sql_items.sqlite");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM lessons");
DefaultTableModel dtm = (DefaultTableModel) tblOpenLesson.getModel();
dtm.setRowCount(0);
while(rs.next()){
Object o[] = {rs.getString("lesson")};
dtm.addRow(o);
}
} catch (ClassNotFoundException | SQLException e) {
}
}
// Variables declaration - do not modify
private javax.swing.JButton btnCancelOpenLesson;
private javax.swing.JButton btnOpenLessonPopUp;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JPanel pnlOpenLesson;
private javax.swing.JTable tblOpenLesson;
// End of variables declaration
}
我开始认为这是不可能的。如果是这样,请告诉我该怎么办。非常感谢
问题来源:Stack Overflow
在getSelectedColumn()/ getSelectedRow ()之前调用updateDBlessons()会将选定的行/列重置为-1。
不要在btnOpenLessonPopUpActionPerformed中调用updateDBlessons(),而是在显示JFrame之前先调用它。
回答来源:Stack Overflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。