我正在尝试操作本地sqlite db中的数据。我有一个jList
组件从db获取数据。
首先我定义一个DefaultListModel
,然后我用addElement方法在其中添加数据。然后我设置jList.setModel('model')
..在我添加或删除数据库jList
中的数据后,重点关注并触发valueChanged事件并设置selectedItemIndex = -1
并输出
线程“AWT-EventQueue-0”中的异常java.lang.ArrayIndexOutOfBoundsException:-1 at java.util.ArrayList.elementData(ArrayList.java:418) at java.util.ArrayList.get(ArrayList.java:431) at notdefteri.NotDefteri.listBaslikValueChanged(NotDefteri.java:75) 在notdefteri.NotDefteri.access 100美元(NotDefteri.java:20) at notdefteri.NotDefteri $ 2.valueChanged(NotDefteri.java:125) 在javax.swing.JList.fireSelectionValueChanged(JList.java:1796) 在javax.swing.JList $ ListSelectionHandler.valueChanged(JList.java:1810) 在javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:184) 在javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:164) 在javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:211) 在javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:405) 在javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:415) at javax.swing.DefaultListSelectionModel.removeSelectionIntervalImpl(DefaultListSelectionModel.java:576) 在javax.swing.DefaultListSelectionModel.clearSelection(DefaultListSelectionModel.java:420) 在javax.swing.JList.clearSelection(JList.java:2043) 在javax.swing.JList.setModel(JList.java:1677) 在notdefteri.NotDefteri.notlariBaslat(NotDefteri.java:55) at notdefteri.NotDefteri.listBaslikMouseClicked(NotDefteri.java:292) 在notdefteri.NotDefteri.access $ 000(NotDefteri.java:20) at notdefteri.NotDefteri $ 1.mouseClicked(NotDefteri.java:120) 在java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270) at java.awt.Component.processMouseEvent(Component.java:6538) 在javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6300) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4891) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4713) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) at java.awt.Container.dispatchEventImpl(Container.java:2280) at java.awt.Component.dispatchEvent(Component.java:4713) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access $ 500(EventQueue.java:97) at java.awt.EventQueue $ 3.run(EventQueue.java:709) at java.awt.EventQueue $ 3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue $ 4.run(EventQueue.java:731) at java.awt.EventQueue $ 4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) 在java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
它继续工作,但我不知道为什么或为什么不工作。
这是我的代码:
package notdefteri;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class NotDefteri extends javax.swing.JFrame {
/**
* Creates new form NotDefteri
*/
SQLiteFunctions sqlf = new SQLiteFunctions();
ResultSet rs;
String textAreaSituation = "";
int selectedListIndex;
ArrayList<Not> siraliNotlar;
int guncelIndex;
public NotDefteri() {
initComponents();
notlariBaslat();
}
public void notlariBaslat() {
DefaultListModel<String> notBasliklari = new DefaultListModel<>();
siraliNotlar = new ArrayList();
rs = sqlf.notlariGetir();
try {
while (rs.next()) {
notBasliklari.addElement(rs.getString("baslik"));
Not siraliNot = new Not(rs.getInt("id"), rs.getString("baslik"), rs.getString("not"));
siraliNotlar.add(siraliNot);
}
listBaslik.setModel(notBasliklari);
listBaslik.setSelectedIndex(0);
} catch (SQLException ex) {
Logger.getLogger(NotDefteri.class.getName()).log(Level.SEVERE, null, ex);
}
}
;
private void listBaslikValueChanged(javax.swing.event.ListSelectionEvent evt) {
// TODO add your handling code here:
if (!evt.getValueIsAdjusting()) {//This line prevents double events
selectedListIndex = listBaslik.getSelectedIndex();
**//It crashes on this line**
Not not = siraliNotlar.get(selectedListIndex);
try {
rs = sqlf.notGetir(not.getDbId());
txtAreaNot.setText(rs.getString("not"));
lblTarih.setText("Tarih : " + rs.getString("tarih"));
} catch (SQLException ex) {
Logger.getLogger(NotDefteri.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}