Search code examples
javaswingjscrollpanejlist

getSelectedIndex() for JList always return -1 eventhough an item is selected


http://prntscr.com/9jhrwa "How the GUI looks"

public class Okno1 extends javax.swing.JFrame {

static Konto[]konto;
static DefaultListModel listModel;
static int indexKonta;
public Okno1() {
    initComponents();
    napolniKonto();
    jScrollPane1.setVisible(false);
    button_potrdiKonto.setVisible(false);       
}

here I fill my array with Objects and add them to DefaultListModel, also I create a new list with the mentioned DefaultListModel

    listModel=new DefaultListModel();
    list_konto.setModel(listModel);
    konto=new Konto[4];
    konto[0]=new Konto("10000/20000", "Test konto primer1");
    konto[1]=new Konto("20000/30000", "Test konto primer2");
    konto[2]=new Konto("50000/60000", "Test konto primer3");
    konto[3]=new Konto("30000/50000", "Test konto primer4");
    for (int i = 0; i < konto.length; i++) {
        listModel.addElement(konto[i].getID()+" | "+konto[i].getOpis());
    }

    list_konto=new JList(listModel);
    jScrollPane1.repaint();    
}

Here I show the jScrollPanel when this button is pressed, I also show the button which must be pressed if I want to get the index of the selected element in the JList displayed

   private void button_prikaziKontoActionPerformed(java.awt.event.ActionEvent evt) {                                                    
    jScrollPane1.setVisible(true);
    button_potrdiKonto.setVisible(true);
    //revalidate();
    //repaint();
}                

Here I press a button and it should get me the index of the selected item, but it keeps giving me -1 and it doesn't matter if an item on the JList is selected or is not

private void button_potrdiKontoActionPerformed(java.awt.event.ActionEvent evt) {                                                   
    //indexKonta=list_konto.getSelectedIndex();
    text_opisKonta.setText(Integer.toString(list_konto.getSelectedIndex()));
}  

Solution

  • It's not clear where your code is going awry. This compete example may allow you to study the problem in isolation. Also consider adding a ListSelectionListener to see the effect.

    myList.addListSelectionListener((ListSelectionEvent e) -> {
        myLabel.setText(getSelectionIndex());
    });
    

    image

    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import javax.swing.AbstractAction;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.event.ListSelectionEvent;
    
    /** @see http://stackoverflow.com/a/34497773/230513 */
    public class Test extends JPanel {
    
        private final String[] values = {"Value1", "Value2", "Value3", "Value4"};
        private final JList myList = new JList(values);
        private final JLabel myLabel = new JLabel();
    
        public Test() {
            myList.setSelectedIndex(values.length - 1);
            myLabel.setText(getSelectionIndex());
            this.add(myList);
            this.add(myLabel);
            this.add(new JButton(new AbstractAction("Show Selected Index") {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    myLabel.setText(getSelectionIndex());
                }
            }));
        }
    
        private String getSelectionIndex() {
            return String.valueOf(myList.getSelectedIndex());
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(() -> {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.add(new Test());
                f.pack();
                f.setLocationByPlatform(true);
                f.setVisible(true);
            });
        }
    }