jeudi 29 octobre 2015

jList valueChanged event sets selected index to -1

I am trying to manipulate data in my local sqlite db. I have a jList component which takes data from db.

First I define a DefaultListModel and then I am adding data in it with addElement method. Then I set jList.setModel('model').. After I add or remove data from db jList is focused and it fires valueChanged event and sets selectedItemIndex = -1

And it outputs

Exception in thread "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) at notdefteri.NotDefteri.access$100(NotDefteri.java:20) at notdefteri.NotDefteri$2.valueChanged(NotDefteri.java:125) at javax.swing.JList.fireSelectionValueChanged(JList.java:1796) at javax.swing.JList$ListSelectionHandler.valueChanged(JList.java:1810) at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:184) at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:164) at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:211) at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:405) at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:415) at javax.swing.DefaultListSelectionModel.removeSelectionIntervalImpl(DefaultListSelectionModel.java:576) at javax.swing.DefaultListSelectionModel.clearSelection(DefaultListSelectionModel.java:420) at javax.swing.JList.clearSelection(JList.java:2043) at javax.swing.JList.setModel(JList.java:1677) at notdefteri.NotDefteri.notlariBaslat(NotDefteri.java:55) at notdefteri.NotDefteri.listBaslikMouseClicked(NotDefteri.java:292) at notdefteri.NotDefteri.access$000(NotDefteri.java:20) at notdefteri.NotDefteri$1.mouseClicked(NotDefteri.java:120) at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270) at java.awt.Component.processMouseEvent(Component.java:6538) at 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) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

It keeps working but I don't know why or why not.

This is my code:

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

            }


        }  
    }    

Aucun commentaire:

Enregistrer un commentaire