简体   繁体   中英

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

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

图片

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);
        });
    }
}
  • don't use static variables

  • always to test if (list.getSelectedIndex() > -1) {

  • use ListSelectionListener for JList , by always testing if (list.getSelectedIndex() > -1) {

for example (without using ListSelectionListener)

在此处输入图片说明

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;

public class JListAndSelection {

    private JFrame frame = new JFrame();
    private DefaultListModel listModel = new DefaultListModel();
    private JList list = new JList(listModel);
    private JScrollPane scrollPane = new JScrollPane(list);
    private JLabel label = new JLabel("nothing is selected");
    private JButton button1 = new JButton("print me selected value");

    public JListAndSelection() {
        button1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                if (list.getSelectedIndex() > -1) {
                    label.setText((String) list.getSelectedValue());
                } else {
                    label.setText("nothing is selected");
                }
            }
        });

        listModel.addElement("10000/20000 - Test konto primer1");
        listModel.addElement("20000/30000 - Test konto primer2");
        listModel.addElement("50000/60000 - Test konto primer3");
        listModel.addElement("30000/50000 - Test konto primer4");

        list.setVisibleRowCount(5);

        frame.setTitle("JFrame");
        frame.add(label, BorderLayout.NORTH);
        frame.add(scrollPane, BorderLayout.CENTER);
        frame.add(button1, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocation(150, 150);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            new JListAndSelection();
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM